package ghappman import ( "math/rand" "testing" "time" ) func TestGetBeforeSessionTimeout(t *testing.T) { var to time.Duration dl, ok := t.Deadline() if !ok { // test has no timeout, pick a default to = time.Second } else { to = time.Until(dl) } store := newSessionStore(to) store.startSession("bar", []byte(`hash`)) if !store.hasSession("bar") { t.Error("session doesnt exist, but should not have timed out") } } func TestGetAfterSessionTimeout(t *testing.T) { to := 1 * time.Millisecond store := newSessionStore(to) store.startSession("bar", []byte(`hash`)) time.Sleep(to * 2) if store.hasSession("bar") { t.Error("session still exists after timeout") } } func TestEndBeforeSessionTimeout(t *testing.T) { to := 1 * time.Millisecond store := newSessionStore(to) store.startSession("bar", []byte(`hash`)) store.endSession("bar") if store.hasSession("bar") { t.Error("session still exists after manual termination") } } func TestEndAfterSessionTimeout(_ *testing.T) { to := 1 * time.Millisecond store := newSessionStore(to) store.startSession("bar", []byte(`hash`)) time.Sleep(to * 2) store.endSession("bar") } func BenchmarkParallelRW(b *testing.B) { // keys := []string{"foo", "bar"} keys := []string{"foo", "bar", "baz", "etc", "bob"} bptr := []byte(`hash`) to := 1 * time.Microsecond store := newSessionStore(to) b.RunParallel(func(pb *testing.PB) { // #nosec for pb.Next() { // I dont know if this really does anything, but I like the idea of fuzzing // by calling the available functions in random orders. Should work well when // using the race detector f1 := func() { ckey := keys[rand.Intn(len(keys))] store.startSession(ckey, bptr) } f2 := func() { rkey := keys[rand.Intn(len(keys))] store.getSession(rkey) } f3 := func() { dkey := keys[rand.Intn(len(keys))] store.endSession(dkey) } f4 := func() { ukey := keys[rand.Intn(len(keys))] store.updateSession(ukey, &AppConfig{}) } f5 := func() { hkey := keys[rand.Intn(len(keys))] store.hasSession(hkey) } fs := []func(){f1, f2, f3, f4, f5} rand.Shuffle(len(fs), func(i, j int) { fs[i], fs[j] = fs[j], fs[i] }) for _, f := range fs { f() } } }) }