From da1b42f9faed89c0e7a750cb0b98dda97306b532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 16 Jun 2022 15:42:52 +0200 Subject: [PATCH] Worker: fix sqlite connection issue in unit tests Fix sqlite issues in the "upstream buffer" test. The test used `:memory:` to have an in-memory DB to separate from other tests. The "flush at shutdown" code runs in a different goroutine, though, and creates a new DB connection. The SQLite separation was too strong, making that function not find any tables. This is now solved by having an in-memory database that's shared between all connections made from the same unit test. --- internal/worker/upstream_buffer_test.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/worker/upstream_buffer_test.go b/internal/worker/upstream_buffer_test.go index 7348d939..ac4df6d8 100644 --- a/internal/worker/upstream_buffer_test.go +++ b/internal/worker/upstream_buffer_test.go @@ -5,7 +5,7 @@ package worker import ( "context" "errors" - "os" + "fmt" "testing" "github.com/benbjohnson/clock" @@ -17,8 +17,6 @@ import ( "git.blender.org/flamenco/pkg/api" ) -const testBufferDBFilename = ":memory:" - type UpstreamBufferDBMocks struct { client *mocks.MockFlamencoClient clock *clock.Mock @@ -30,9 +28,6 @@ func mockUpstreamBufferDB(t *testing.T, mockCtrl *gomock.Controller) (*UpstreamB clock: clock.NewMock(), } - // Always start tests with a fresh database. - os.Remove(testBufferDBFilename) - ub, err := NewUpstreamBuffer(mocks.client, mocks.clock) if err != nil { t.Fatalf("unable to create upstream buffer: %v", err) @@ -41,6 +36,13 @@ func mockUpstreamBufferDB(t *testing.T, mockCtrl *gomock.Controller) (*UpstreamB return ub, &mocks } +// sqliteTestDBName returns a DSN for SQLite that separates tests from each +// other, but lets all connections made within the same test to connect to the +// same in-memory instance. +func sqliteTestDBName(t *testing.T) string { + return fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name()) +} + func TestUpstreamBufferCloseUnopened(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() @@ -57,7 +59,7 @@ func TestUpstreamBufferManagerUnavailable(t *testing.T) { ctx := context.Background() ub, mocks := mockUpstreamBufferDB(t, mockCtrl) - assert.NoError(t, ub.OpenDB(ctx, testBufferDBFilename)) + assert.NoError(t, ub.OpenDB(ctx, sqliteTestDBName(t))) // Send a task update without Manager available. taskID := "3960dec4-978e-40ab-bede-bfa6428c6ebc"