21703: Merge branch 'main' into 21703-collection-update-lock
[arvados.git] / lib / controller / localdb / log_activity_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package localdb
6
7 import (
8         "database/sql"
9         "time"
10
11         "git.arvados.org/arvados.git/sdk/go/arvados"
12         "git.arvados.org/arvados.git/sdk/go/arvadostest"
13         check "gopkg.in/check.v1"
14 )
15
16 var _ = check.Suite(&activityPeriodSuite{})
17
18 type activityPeriodSuite struct{}
19
20 // The important thing is that, even when daylight savings time is
21 // making things difficult, the current period ends in the future.
22 func (*activityPeriodSuite) TestPeriod(c *check.C) {
23         toronto, err := time.LoadLocation("America/Toronto")
24         c.Assert(err, check.IsNil)
25
26         format := "2006-01-02 15:04:05 MST"
27         dststartday, err := time.ParseInLocation(format, "2022-03-13 00:00:00 EST", toronto)
28         c.Assert(err, check.IsNil)
29         dstendday, err := time.ParseInLocation(format, "2022-11-06 00:00:00 EDT", toronto)
30         c.Assert(err, check.IsNil)
31
32         for _, period := range []time.Duration{
33                 time.Minute * 13,
34                 time.Minute * 49,
35                 time.Hour,
36                 4 * time.Hour,
37                 48 * time.Hour,
38         } {
39                 for offset := time.Duration(0); offset < 48*time.Hour; offset += 3 * time.Minute {
40                         t := dststartday.Add(offset)
41                         end := alignedPeriod(t, period)
42                         c.Check(end.After(t), check.Equals, true, check.Commentf("period %v offset %v", period, offset))
43
44                         t = dstendday.Add(offset)
45                         end = alignedPeriod(t, period)
46                         c.Check(end.After(t), check.Equals, true, check.Commentf("period %v offset %v", period, offset))
47                 }
48         }
49 }
50
51 func (s *CollectionSuite) TestLogActivity(c *check.C) {
52         starttime := time.Now()
53         s.localdb.activeUsersLock.Lock()
54         s.localdb.activeUsersReset = starttime
55         s.localdb.activeUsersLock.Unlock()
56         for i := 0; i < 2; i++ {
57                 logthreshold := time.Now()
58                 _, err := s.localdb.CollectionCreate(s.userctx, arvados.CreateOptions{
59                         Attrs: map[string]interface{}{
60                                 "name": "test collection",
61                         },
62                         EnsureUniqueName: true,
63                 })
64                 c.Assert(err, check.IsNil)
65                 var uuid string
66                 err = s.db.QueryRowContext(s.ctx, `select uuid from logs where object_uuid = $1 and event_at > $2`, arvadostest.ActiveUserUUID, logthreshold.UTC()).Scan(&uuid)
67                 if i == 0 {
68                         c.Check(err, check.IsNil)
69                         c.Check(uuid, check.HasLen, 27)
70                 } else {
71                         c.Check(err, check.Equals, sql.ErrNoRows)
72                 }
73         }
74 }