@@ -6,28 +6,20 @@ import (
66 "testing"
77 "time"
88
9- "github.com/launchdarkly /go-server-sdk/v7/testhelpers/ldtestdata "
9+ "github.com/redis /go-redis/v9 "
1010 "github.com/stretchr/testify/assert"
1111 "github.com/stretchr/testify/require"
1212
13- "github.com/e2b-dev/infra/packages/shared/pkg/featureflags"
1413 redis_utils "github.com/e2b-dev/infra/packages/shared/pkg/redis"
1514)
1615
17- func TestRedisCatalog_LocalCacheFlagServiceContext (t * testing.T ) {
18- t .Parallel ()
16+ func seedRedisCatalogEntry (t * testing.T ) (context. Context , redis. UniversalClient , string , * SandboxInfo ) {
17+ t .Helper ()
1918
2019 ctx := t .Context ()
2120 redisClient := redis_utils .SetupInstance (t )
2221
23- source := ldtestdata .DataSource ()
24- source .Update (
25- source .Flag (featureflags .SandboxCatalogLocalCacheFlag .Key ()).
26- VariationForAll (true ).
27- VariationForKey (featureflags .ServiceKind , "client-proxy" , false ),
28- )
29-
30- sbxID := "sbx-service-context"
22+ sbxID := "sbx-local-cache"
3123 expected := & SandboxInfo {
3224 OrchestratorID : "orch-1" ,
3325 OrchestratorIP : "10.0.0.1" ,
@@ -40,52 +32,43 @@ func TestRedisCatalog_LocalCacheFlagServiceContext(t *testing.T) {
4032 require .NoError (t , err )
4133 require .NoError (t , redisClient .Set (ctx , "sandbox:catalog:" + sbxID , data , time .Minute ).Err ())
4234
43- t .Run ("service-targeted disable prevents local cache write" , func (t * testing.T ) {
44- t .Parallel ()
45- ctx := t .Context ()
46-
47- ff , err := featureflags .NewClientWithDatasource (source )
48- require .NoError (t , err )
49- ff .SetServiceName ("client-proxy" )
50- t .Cleanup (func () {
51- assert .NoError (t , ff .Close (context .Background ()))
52- })
35+ return ctx , redisClient , sbxID , expected
36+ }
5337
54- catalog := NewRedisSandboxesCatalog (redisClient , ff )
55- t .Cleanup (func () {
56- assert .NoError (t , catalog .Close (context .Background ()))
57- })
38+ func TestRedisCatalog_NoopCacheRemainsEmpty (t * testing.T ) {
39+ t .Parallel ()
5840
59- got , err := catalog .GetSandbox (ctx , sbxID )
60- require .NoError (t , err )
61- require .Equal (t , expected .OrchestratorID , got .OrchestratorID )
62- require .Equal (t , expected .ExecutionID , got .ExecutionID )
41+ ctx , redisClient , sbxID , expected := seedRedisCatalogEntry (t )
6342
64- assert .Nil (t , catalog .cache .Get (sbxID ), "local cache should remain empty when flag is disabled for this service" )
43+ cache := NewNoopSandboxCache ()
44+ noCacheCatalog := NewRedisSandboxCatalog (redisClient , cache )
45+ t .Cleanup (func () {
46+ assert .NoError (t , noCacheCatalog .Close (context .Background ()))
6547 })
6648
67- t .Run ("other service uses local cache" , func (t * testing.T ) {
68- t .Parallel ()
69- ctx := t .Context ()
70-
71- ff , err := featureflags .NewClientWithDatasource (source )
72- require .NoError (t , err )
73- ff .SetServiceName ("orchestration-api" )
74- t .Cleanup (func () {
75- assert .NoError (t , ff .Close (context .Background ()))
76- })
77-
78- catalog := NewRedisSandboxesCatalog (redisClient , ff )
79- t .Cleanup (func () {
80- assert .NoError (t , catalog .Close (context .Background ()))
81- })
82-
83- got , err := catalog .GetSandbox (ctx , sbxID )
84- require .NoError (t , err )
85- require .Equal (t , expected .OrchestratorIP , got .OrchestratorIP )
86-
87- item := catalog .cache .Get (sbxID )
88- require .NotNil (t , item , "local cache should be populated when flag is enabled" )
89- require .Equal (t , expected .ExecutionID , item .Value ().ExecutionID )
49+ got , err := noCacheCatalog .GetSandbox (ctx , sbxID )
50+ require .NoError (t , err )
51+ require .Equal (t , expected .OrchestratorID , got .OrchestratorID )
52+ require .Equal (t , expected .ExecutionID , got .ExecutionID )
53+ assert .Nil (t , cache .Get (sbxID ))
54+ }
55+
56+ func TestRedisCatalog_ReadThroughCachePopulatesLocalCache (t * testing.T ) {
57+ t .Parallel ()
58+
59+ ctx , redisClient , sbxID , expected := seedRedisCatalogEntry (t )
60+
61+ cache := NewReadThroughSandboxCache ()
62+ cachedCatalog := NewRedisSandboxCatalog (redisClient , cache )
63+ t .Cleanup (func () {
64+ assert .NoError (t , cachedCatalog .Close (context .Background ()))
9065 })
66+
67+ got , err := cachedCatalog .GetSandbox (ctx , sbxID )
68+ require .NoError (t , err )
69+ require .Equal (t , expected .OrchestratorIP , got .OrchestratorIP )
70+
71+ item := cache .Get (sbxID )
72+ require .NotNil (t , item , "local cache should be populated when enabled" )
73+ require .Equal (t , expected .ExecutionID , item .Value ().ExecutionID )
9174}
0 commit comments