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.
This commit is contained in:
Sybren A. Stüvel 2022-06-16 15:42:52 +02:00
parent 7e28cfa69c
commit da1b42f9fa

View File

@ -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"