From e0fc3e20c13f90b2129cd6b55e52bedc485633c6 Mon Sep 17 00:00:00 2001 From: Shimanshu Date: Mon, 28 Oct 2024 00:03:17 +0530 Subject: [PATCH] Integration test for cleanup cron (#51) --- .../cleanup-cron/cleanup_cron_test.go | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 internal/tests/integration/cleanup-cron/cleanup_cron_test.go diff --git a/internal/tests/integration/cleanup-cron/cleanup_cron_test.go b/internal/tests/integration/cleanup-cron/cleanup_cron_test.go new file mode 100644 index 0000000..dfdebf0 --- /dev/null +++ b/internal/tests/integration/cleanup-cron/cleanup_cron_test.go @@ -0,0 +1,93 @@ +package cleanup_cron + +import ( + "context" + "fmt" + "net" + "server/config" + "server/internal/db" + "server/internal/server" + "server/internal/server/utils" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + setup_test "server/internal/tests/integration/setup" +) + +// getDiceDBClient initializes the DiceDB client using the container's exposed port +func getDiceDBClient(container *setup_test.DiceDBContainer, config *config.Config, isAdmin bool) (*db.DiceDB, error) { + // Get the container's host and port + host, err := container.Container.Host(context.Background()) + if err != nil { + return nil, fmt.Errorf("failed to get host: %v", err) + } + + port, err := container.Container.MappedPort(context.Background(), "7379/tcp") + if err != nil { + return nil, fmt.Errorf("failed to get mapped port: %v", err) + } + + address := net.JoinHostPort(host, port.Port()) + // the port is dynamically assigned so that's why + // changing addr of diceDb and diceDbAdmin + if isAdmin == true { + config.DiceDBAdmin.Addr = address + } else { + config.DiceDB.Addr = address + } + + client, err := db.InitDiceClient(config, isAdmin) + if err != nil { + return nil, fmt.Errorf("failed to create DiceDB client: %v", err) + } + + return client, nil +} + +func TestStartCleanupCronWithDiceDB(t *testing.T) { + ctx := context.Background() + + configValue := config.LoadConfig() + + diceDbContainer, err := setup_test.InitializeDiceDBContainer(ctx) + assert.NoError(t, err, "should initialize DiceDB container") + defer diceDbContainer.Cleanup(ctx) + + diceDbAdminContainer, err := setup_test.InitializeDiceDBContainer(ctx) + assert.NoError(t, err, "should initialize Admin DiceDB container") + defer diceDbAdminContainer.Cleanup(ctx) + + // Get userDemoClient and sysDiceClient from the running container + diceDb, err := getDiceDBClient(diceDbContainer, configValue, false) + assert.NoError(t, err, "should create DiceDB client") + + diceDbAdmin, err := getDiceDBClient(diceDbAdminContainer, configValue, true) + assert.NoError(t, err, "should create Admin DiceDB client") + + // Add a sample key to test cleanup + sampleValueResp := diceDb.Client.Set(ctx, "sample", "dummy", -1) + assert.NoError(t, sampleValueResp.Err(), "should set sample key in DiceDB") + + wg := sync.WaitGroup{} + // Register a cleanup manager, this runs user DiceDB instance cleanup job at configured frequency + // setting a frequency for a second so that I can easily test it. + cleanupManager := server.NewCleanupManager(diceDbAdmin, diceDb, time.Second) + wg.Add(1) + go cleanupManager.Run(ctx, &wg) + + time.Sleep(2 * time.Second) + + // Check if any keys are still present in diceDb (should be cleaned up) + response := diceDb.Client.Keys(ctx, "*") + assert.NoError(t, response.Err(), "should execute Keys command on DiceDB") + assert.Equal(t, response.Val(), []string{}, "should have cleaned up keys in diceDb") + + // Retrieve the last cleanup time from diceDbAdmin + cleanupTimeResp := diceDbAdmin.Client.Get(ctx, utils.LastCronCleanupTimeUnixMs) + assert.NoError(t, cleanupTimeResp.Err(), "should execute GET command on adminDiceDb") + assert.NotEmpty(t, cleanupTimeResp.Val(), "should have set playground_mono:last_cron_cleanup_run_time_unix_ms in diceDbAdmin") + +}