1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/config"
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/arvadostest"
17 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 "github.com/jmoiron/sqlx"
19 "github.com/sirupsen/logrus"
20 check "gopkg.in/check.v1"
23 func Test(t *testing.T) {
27 var _ = check.Suite(&suite{})
30 cluster *arvados.Cluster
32 getdb func(context.Context) (*sqlx.DB, error)
35 var testLocker = &DBLocker{key: 999}
37 func (s *suite) SetUpSuite(c *check.C) {
38 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
39 c.Assert(err, check.IsNil)
40 s.cluster, err = cfg.GetCluster("")
41 c.Assert(err, check.IsNil)
42 s.db = arvadostest.DB(c, s.cluster)
43 s.getdb = func(context.Context) (*sqlx.DB, error) { return s.db, nil }
46 func (s *suite) TestLock(c *check.C) {
47 retryDelay = 10 * time.Millisecond
49 var logbuf bytes.Buffer
50 logger := ctxlog.New(&logbuf, "text", "debug")
51 logger.Level = logrus.DebugLevel
52 ctx := ctxlog.Context(context.Background(), logger)
53 ctx, cancel := context.WithCancel(ctx)
55 testLocker.Lock(ctx, s.getdb)
58 lock2 := make(chan bool)
64 testLocker2 := &DBLocker{key: 999}
65 testLocker2.Lock(ctx, s.getdb)
71 // Second lock should wait for first to Unlock
73 case <-time.After(time.Second / 10):
74 c.Check(logbuf.String(), check.Matches, `(?ms).*level=info.*DBClient="[^"]+:\d+".*ID=999.*`)
83 // Now the second lock should succeed within retryDelay
85 case <-time.After(retryDelay * 2):
90 c.Logf("%s", logbuf.String())