github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/logger/debugger_mem_test.go (about) 1 package logger 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestLogger_MemDebugger_Success(t *testing.T) { 12 dbg := NewMemDebugger() 13 14 _ = dbg.AddDomain("foo", time.Second) 15 16 expirationDate := dbg.ExpiresAt("foo") 17 assert.NotNil(t, expirationDate) 18 } 19 20 func TestLogger_MemDebugger_Delete(t *testing.T) { 21 dbg := NewMemDebugger() 22 23 _ = dbg.AddDomain("foo", time.Second) 24 25 _ = dbg.RemoveDomain("foo") 26 27 expirationDate := dbg.ExpiresAt("foo") 28 assert.Nil(t, expirationDate) 29 } 30 31 func TestLogger_MemDebugger_Expire(t *testing.T) { 32 dbg := NewMemDebugger() 33 34 _ = dbg.AddDomain("foo", 2*time.Millisecond) 35 36 time.Sleep(10 * time.Millisecond) 37 38 expirationDate := dbg.ExpiresAt("foo") 39 assert.Nil(t, expirationDate) 40 } 41 42 func TestLogger_MemDebugger_Override_and_expire(t *testing.T) { 43 dbg := NewMemDebugger() 44 45 // First setup a domain with a 1s TTL 46 dbg.AddDomain("foo", time.Second) 47 48 // Check that we have 1s 49 expirationDate := dbg.ExpiresAt("foo") 50 require.NotNil(t, expirationDate) 51 require.WithinDuration(t, time.Now().Add(time.Second), *expirationDate, 5*time.Millisecond) 52 53 // Then move the domain to 2ms TTL 54 _ = dbg.AddDomain("foo", 2*time.Millisecond) 55 56 // Check that we have 2ms 57 expirationDate = dbg.ExpiresAt("foo") 58 require.NotNil(t, expirationDate) 59 require.WithinDuration(t, time.Now().Add(2*time.Millisecond), *expirationDate, 2*time.Millisecond) 60 61 time.Sleep(10 * time.Millisecond) 62 63 // Check the correct expiration 64 expirationDate = dbg.ExpiresAt("foo") 65 assert.Nil(t, expirationDate) 66 }