...

Source file src/edge-infra.dev/pkg/f8n/devinfra/ghappman/session_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/ghappman

     1  package ghappman
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestGetBeforeSessionTimeout(t *testing.T) {
    10  	var to time.Duration
    11  	dl, ok := t.Deadline()
    12  	if !ok {
    13  		// test has no timeout, pick a default
    14  		to = time.Second
    15  	} else {
    16  		to = time.Until(dl)
    17  	}
    18  	store := newSessionStore(to)
    19  	store.startSession("bar", []byte(`hash`))
    20  	if !store.hasSession("bar") {
    21  		t.Error("session doesnt exist, but should not have timed out")
    22  	}
    23  }
    24  
    25  func TestGetAfterSessionTimeout(t *testing.T) {
    26  	to := 1 * time.Millisecond
    27  	store := newSessionStore(to)
    28  	store.startSession("bar", []byte(`hash`))
    29  	time.Sleep(to * 2)
    30  	if store.hasSession("bar") {
    31  		t.Error("session still exists after timeout")
    32  	}
    33  }
    34  
    35  func TestEndBeforeSessionTimeout(t *testing.T) {
    36  	to := 1 * time.Millisecond
    37  	store := newSessionStore(to)
    38  	store.startSession("bar", []byte(`hash`))
    39  	store.endSession("bar")
    40  	if store.hasSession("bar") {
    41  		t.Error("session still exists after manual termination")
    42  	}
    43  }
    44  
    45  func TestEndAfterSessionTimeout(_ *testing.T) {
    46  	to := 1 * time.Millisecond
    47  	store := newSessionStore(to)
    48  	store.startSession("bar", []byte(`hash`))
    49  	time.Sleep(to * 2)
    50  	store.endSession("bar")
    51  }
    52  
    53  func BenchmarkParallelRW(b *testing.B) {
    54  	// keys := []string{"foo", "bar"}
    55  	keys := []string{"foo", "bar", "baz", "etc", "bob"}
    56  	bptr := []byte(`hash`)
    57  	to := 1 * time.Microsecond
    58  	store := newSessionStore(to)
    59  	b.RunParallel(func(pb *testing.PB) {
    60  		// #nosec
    61  		for pb.Next() {
    62  			// I dont know if this really does anything, but I like the idea of fuzzing
    63  			// by calling the available functions in random orders. Should work well when
    64  			// using the race detector
    65  			f1 := func() {
    66  				ckey := keys[rand.Intn(len(keys))]
    67  				store.startSession(ckey, bptr)
    68  			}
    69  			f2 := func() {
    70  				rkey := keys[rand.Intn(len(keys))]
    71  				store.getSession(rkey)
    72  			}
    73  			f3 := func() {
    74  				dkey := keys[rand.Intn(len(keys))]
    75  				store.endSession(dkey)
    76  			}
    77  			f4 := func() {
    78  				ukey := keys[rand.Intn(len(keys))]
    79  				store.updateSession(ukey, &AppConfig{})
    80  			}
    81  			f5 := func() {
    82  				hkey := keys[rand.Intn(len(keys))]
    83  				store.hasSession(hkey)
    84  			}
    85  			fs := []func(){f1, f2, f3, f4, f5}
    86  			rand.Shuffle(len(fs), func(i, j int) { fs[i], fs[j] = fs[j], fs[i] })
    87  			for _, f := range fs {
    88  				f()
    89  			}
    90  		}
    91  	})
    92  }
    93  

View as plain text