From cc72c9c054536a8b3a05822e49010a70bf805913 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Tue, 6 Aug 2024 00:06:05 -0600 Subject: [PATCH] Fix expired demo query logic. Use TempDir for test asset storage --- internal/demo/demo_respository.go | 2 +- internal/demo/demo_usecase.go | 6 +++--- internal/domain/demo.go | 1 + internal/test/demos_test.go | 29 ++++++++++++++++++++--------- internal/test/main_test.go | 4 +++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/internal/demo/demo_respository.go b/internal/demo/demo_respository.go index f6037a60..757aaee7 100644 --- a/internal/demo/demo_respository.go +++ b/internal/demo/demo_respository.go @@ -26,7 +26,7 @@ func (r *demoRepository) ExpiredDemos(ctx context.Context, limit uint64) ([]doma LeftJoin("report r on d.demo_id = r.demo_id"). Where("d.archive = false"). OrderBy("d.demo_id"). - Offset(limit)) + Limit(limit)) if errRow != nil { return nil, r.db.DBErr(errRow) } diff --git a/internal/demo/demo_usecase.go b/internal/demo/demo_usecase.go index bc5a484a..82ae30f9 100644 --- a/internal/demo/demo_usecase.go +++ b/internal/demo/demo_usecase.go @@ -151,7 +151,7 @@ func (d demoUsecase) TruncateByCount(ctx context.Context, maxCount uint64) (int, return count, size, nil } -func (d demoUsecase) executeCleanup(ctx context.Context) { +func (d demoUsecase) Cleanup(ctx context.Context) { conf := d.config.Config() if !conf.Demo.DemoCleanupEnabled { @@ -188,7 +188,7 @@ func (d demoUsecase) Start(ctx context.Context) { ticker := time.NewTicker(time.Hour) tickerOrphans := time.NewTicker(time.Hour * 24) - d.executeCleanup(ctx) + d.Cleanup(ctx) if err := d.RemoveOrphans(ctx); err != nil { slog.Error("Failed to execute orphans", log.ErrAttr(err)) @@ -199,7 +199,7 @@ func (d demoUsecase) Start(ctx context.Context) { case <-ticker.C: d.cleanupChan <- true case <-d.cleanupChan: - d.executeCleanup(ctx) + d.Cleanup(ctx) case <-tickerOrphans.C: if err := d.RemoveOrphans(ctx); err != nil { slog.Error("Failed to execute orphans", log.ErrAttr(err)) diff --git a/internal/domain/demo.go b/internal/domain/demo.go index c9ec9f7d..b4ae3b27 100644 --- a/internal/domain/demo.go +++ b/internal/domain/demo.go @@ -17,6 +17,7 @@ type DemoUsecase interface { GetDemos(ctx context.Context) ([]DemoFile, error) CreateFromAsset(ctx context.Context, asset Asset, serverID int) (*DemoFile, error) TriggerCleanup() + Cleanup(ctx context.Context) } type DemoRepository interface { diff --git a/internal/test/demos_test.go b/internal/test/demos_test.go index ded2a50c..deffdc16 100644 --- a/internal/test/demos_test.go +++ b/internal/test/demos_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "fmt" + "os" "testing" "github.com/leighmacdonald/gbans/internal/demo" @@ -13,8 +14,19 @@ import ( func TestDemosCleanup(t *testing.T) { ctx := context.Background() + + tempDir, errDir := os.MkdirTemp("", "test-assets") + require.NoError(t, errDir) + + conf := configUC.Config() + conf.LocalStore.PathRoot = tempDir + conf.Demo.DemoCleanupEnabled = true + conf.Demo.DemoCleanupStrategy = domain.DemoStrategyCount + conf.Demo.DemoCountLimit = 5 + + require.NoError(t, configUC.Write(ctx, conf)) + fetcher := demo.NewFetcher(tempDB, configUC, serversUC, assetUC, demoUC) - go demoUC.Start(ctx) for demoNum := range 10 { content := make([]byte, 100000) @@ -28,16 +40,15 @@ func TestDemosCleanup(t *testing.T) { })) } - conf := configUC.Config() - conf.Demo.DemoCleanupEnabled = true - conf.Demo.DemoCleanupStrategy = domain.DemoStrategyCount - conf.Demo.DemoCountLimit = 5 - - require.NoError(t, configUC.Write(ctx, conf)) + expired, errExpired := demoRepository.ExpiredDemos(ctx, 5) + require.NoError(t, errExpired) + for _, expiredDemo := range expired { + require.Less(t, expiredDemo.DemoID, int64(6)) + } - demoUC.TriggerCleanup() + demoUC.Cleanup(ctx) allDemos, err := demoUC.GetDemos(ctx) require.NoError(t, err) - require.Len(t, len(allDemos), 5) + require.Len(t, allDemos, 5) } diff --git a/internal/test/main_test.go b/internal/test/main_test.go index 3bf5893e..f5314973 100644 --- a/internal/test/main_test.go +++ b/internal/test/main_test.go @@ -69,6 +69,7 @@ var ( banNetUC domain.BanNetUsecase assetUC domain.AssetUsecase chatUC domain.ChatUsecase + demoRepository domain.DemoRepository demoUC domain.DemoUsecase discordUC domain.DiscordUsecase forumUC domain.ForumUsecase @@ -141,7 +142,8 @@ func TestMain(m *testing.M) { stateUC = state.NewStateUsecase(eventBroadcaster, state.NewStateRepository(state.NewCollector(serversUC)), configUC, serversUC) networkUC = network.NewNetworkUsecase(eventBroadcaster, network.NewNetworkRepository(databaseConn), personUC, configUC) - demoUC = demo.NewDemoUsecase("demos", demo.NewDemoRepository(databaseConn), assetUC, configUC, serversUC) + demoRepository = demo.NewDemoRepository(databaseConn) + demoUC = demo.NewDemoUsecase("demos", demoRepository, assetUC, configUC, serversUC) reportUC = report.NewReportUsecase(report.NewReportRepository(databaseConn), discordUC, configUC, personUC, demoUC) banSteamUC = ban.NewBanSteamUsecase(ban.NewBanSteamRepository(databaseConn, personUC, networkUC), personUC, configUC, discordUC, reportUC, stateUC) authUC = auth.NewAuthUsecase(authRepo, configUC, personUC, banSteamUC, serversUC)