16636: Merge branch 'master' into 16636-more-metrics
[arvados.git] / services / keep-balance / collection_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "context"
9         "sync"
10         "time"
11
12         "git.arvados.org/arvados.git/sdk/go/arvados"
13         check "gopkg.in/check.v1"
14 )
15
16 //  TestIdenticalTimestamps ensures EachCollection returns the same
17 //  set of collections for various page sizes -- even page sizes so
18 //  small that we get entire pages full of collections with identical
19 //  timestamps and exercise our gettingExactTimestamp cases.
20 func (s *integrationSuite) TestIdenticalTimestamps(c *check.C) {
21         // pageSize==0 uses the default (large) page size.
22         pageSizes := []int{0, 2, 3, 4, 5}
23         got := make([][]string, len(pageSizes))
24         var wg sync.WaitGroup
25         for trial, pageSize := range pageSizes {
26                 wg.Add(1)
27                 go func(trial, pageSize int) {
28                         defer wg.Done()
29                         streak := 0
30                         longestStreak := 0
31                         var lastMod time.Time
32                         sawUUID := make(map[string]bool)
33                         err := EachCollection(context.Background(), s.client, pageSize, func(c arvados.Collection) error {
34                                 if c.ModifiedAt.IsZero() {
35                                         return nil
36                                 }
37                                 if sawUUID[c.UUID] {
38                                         // dup
39                                         return nil
40                                 }
41                                 got[trial] = append(got[trial], c.UUID)
42                                 sawUUID[c.UUID] = true
43                                 if lastMod == c.ModifiedAt {
44                                         streak++
45                                         if streak > longestStreak {
46                                                 longestStreak = streak
47                                         }
48                                 } else {
49                                         streak = 0
50                                         lastMod = c.ModifiedAt
51                                 }
52                                 return nil
53                         }, nil)
54                         c.Check(err, check.IsNil)
55                         c.Check(longestStreak > 25, check.Equals, true)
56                 }(trial, pageSize)
57         }
58         wg.Wait()
59         for trial := 1; trial < len(pageSizes); trial++ {
60                 c.Check(got[trial], check.DeepEquals, got[0])
61         }
62 }