Skip to content

Commit

Permalink
Fix expired demo query logic. Use TempDir for test asset storage
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Aug 6, 2024
1 parent 7c5d3d0 commit cc72c9c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/demo/demo_respository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/demo/demo_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down
1 change: 1 addition & 0 deletions internal/domain/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 20 additions & 9 deletions internal/test/demos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"fmt"
"os"
"testing"

"github.com/leighmacdonald/gbans/internal/demo"
Expand All @@ -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)
Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion internal/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cc72c9c

Please sign in to comment.