Skip to content

Commit

Permalink
test: fix flaky test
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zeng <[email protected]>
  • Loading branch information
knight42 committed Jan 2, 2024
1 parent 50ab69f commit 96c98bb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
25 changes: 0 additions & 25 deletions pkg/cron/cron_test.go

This file was deleted.

10 changes: 10 additions & 0 deletions pkg/docker/fake/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/docker/docker/api/types"
Expand All @@ -15,10 +16,13 @@ import (
)

type Client struct {
mu sync.Mutex
containers []types.Container
}

func (f *Client) RunContainer(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (id string, err error) {
f.mu.Lock()
defer f.mu.Unlock()
for _, ct := range f.containers {
if ct.Names[0] == containerName {
return "", errdefs.Conflict(errors.New("container already exists"))
Expand All @@ -40,6 +44,8 @@ func (f *Client) PullImage(ctx context.Context, image string) error {
}

func (f *Client) WaitContainer(ctx context.Context, id string) (int, error) {
f.mu.Lock()
defer f.mu.Unlock()
for i, ct := range f.containers {
if ct.ID == id {
time.Sleep(5 * time.Second)
Expand All @@ -51,6 +57,8 @@ func (f *Client) WaitContainer(ctx context.Context, id string) (int, error) {
}

func (f *Client) RemoveContainerWithTimeout(id string, timeout time.Duration) error {
f.mu.Lock()
defer f.mu.Unlock()
cts := make([]types.Container, 0, len(f.containers))
for _, ct := range f.containers {
if ct.ID != id {
Expand All @@ -62,6 +70,8 @@ func (f *Client) RemoveContainerWithTimeout(id string, timeout time.Duration) er
}

func (f *Client) ListContainersWithTimeout(running bool, timeout time.Duration) ([]types.Container, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.containers, nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ func New(configPath string) (*Server, error) {
}

func NewWithConfig(cfg Config) (*Server, error) {
// TODO: enforce shared cache mode?
db, err := gorm.Open(sqlite.Open(cfg.DbURL), &gorm.Config{
QueryFields: true,
QueryFields: true,
SkipDefaultTransaction: true,
})
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
Expand Down
8 changes: 7 additions & 1 deletion pkg/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ func NewTestEnv(t *testing.T) *TestEnv {
_ = os.Remove(dbFile.Name())
})
db, err := gorm.Open(sqlite.Open(dbFile.Name()), &gorm.Config{
QueryFields: true,
QueryFields: true,
SkipDefaultTransaction: true,
})
require.NoError(t, err)
sqlDB, err := db.DB()
require.NoError(t, err)
// To resolve the "database is locked" error.
// See also https://github.com/mattn/go-sqlite3/issues/209
sqlDB.SetMaxOpenConns(1)
require.NoError(t, model.AutoMigrate(db))

s := &Server{
Expand Down
5 changes: 4 additions & 1 deletion pkg/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (s *Server) waitForSync(name, ctID, storageDir string) {
code = -2
}
}
s.syncingContainers.Delete(name)
err = s.dockerCli.RemoveContainerWithTimeout(ctID, time.Second*20)
if err != nil {
l.Error("Fail to remove container", slogErrAttr(err))
Expand All @@ -119,6 +118,10 @@ func (s *Server) waitForSync(name, ctID, storageDir string) {
l.Error("Fail to update RepoMeta", slogErrAttr(err))
}

// NOTE: Only change status after RepoMeta is updated, b/c we need to determine
// if synchronization is completed in unit test, and then verify RepoMeta.
s.syncingContainers.Delete(name)

if len(s.config.PostSync) == 0 {
return
}
Expand Down

0 comments on commit 96c98bb

Please sign in to comment.