github.com/mailgun/holster/v4@v4.20.0/httpsign/nonce_test.go (about) 1 package httpsign 2 3 import ( 4 "testing" 5 6 "github.com/mailgun/holster/v4/clock" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestNonceInCache(t *testing.T) { 11 clock.Freeze(clock.Now()) 12 defer clock.Unfreeze() 13 14 // setup 15 nc, err := newNonceCache( 16 100, 17 1, 18 ) 19 if err != nil { 20 t.Error("Got unexpected error from newNonceCache:", err) 21 } 22 23 // nothing in cache, it should be valid 24 inCache, err := nc.inCache("0") 25 require.NoError(t, err) 26 if inCache { 27 t.Error("Check should be valid, but failed.") 28 } 29 30 // second time around it shouldn't be 31 inCache, err = nc.inCache("0") 32 require.NoError(t, err) 33 if !inCache { 34 t.Error("Check should be invalid, but passed.") 35 } 36 37 // check some other value 38 clock.Advance(999 * clock.Millisecond) 39 inCache, err = nc.inCache("1") 40 require.NoError(t, err) 41 if inCache { 42 t.Error("Check should be valid, but failed.", err) 43 } 44 45 // age off first value, then it should be valid 46 clock.Advance(1 * clock.Millisecond) 47 inCache, err = nc.inCache("0") 48 require.NoError(t, err) 49 if inCache { 50 t.Error("Check should be valid, but failed.") 51 } 52 }