OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package tumble |
| 6 |
| 7 import ( |
| 8 "math" |
| 9 "testing" |
| 10 "time" |
| 11 |
| 12 "github.com/luci/gae/impl/memory" |
| 13 "github.com/luci/gae/service/taskqueue" |
| 14 "github.com/luci/luci-go/common/clock/testclock" |
| 15 "github.com/luci/luci-go/common/logging" |
| 16 "github.com/luci/luci-go/common/logging/memlogger" |
| 17 . "github.com/smartystreets/goconvey/convey" |
| 18 "golang.org/x/net/context" |
| 19 ) |
| 20 |
| 21 var _ = math.E |
| 22 |
| 23 func TestShardCalculation(t *testing.T) { |
| 24 t.Parallel() |
| 25 |
| 26 Convey("shard calculation", t, func() { |
| 27 numShards := uint64(11) |
| 28 |
| 29 ctx := memlogger.Use(context.Background()) |
| 30 ctx = Use(ctx, Config{NumShards: numShards}) |
| 31 |
| 32 cfg := GetConfig(ctx) |
| 33 |
| 34 l := logging.Get(ctx).(*memlogger.MemLogger) |
| 35 |
| 36 Convey("ExpandedShard->Shard covers the full int64 range", func(
) { |
| 37 tcs := []struct { |
| 38 es int64 |
| 39 s uint64 |
| 40 }{ |
| 41 {math.MinInt64, 0}, |
| 42 {0, numShards / 2}, |
| 43 {math.MaxInt64, numShards - 1}, |
| 44 } |
| 45 |
| 46 for _, tc := range tcs { |
| 47 So((&realMutation{ExpandedShard: tc.es}).shard(&
cfg), ShouldEqual, tc.s) |
| 48 low, high := expandedShardBounds(ctx, tc.s) |
| 49 So(tc.es, ShouldBeGreaterThanOrEqualTo, low) |
| 50 So(tc.es, ShouldBeLessThanOrEqualTo, high) |
| 51 So(l.Messages(), ShouldBeEmpty) |
| 52 } |
| 53 }) |
| 54 |
| 55 Convey("expandedShardsPerShard returns crossed ranges on shard r
eduction", func() { |
| 56 low, high := expandedShardBounds(ctx, 256) |
| 57 So(low, ShouldBeGreaterThan, high) |
| 58 So(l.Messages(), ShouldResemble, []memlogger.LogEntry{ |
| 59 {Level: logging.Warning, Msg: "Invalid shard: 25
6"}, |
| 60 }) |
| 61 }) |
| 62 }) |
| 63 } |
| 64 |
| 65 func TestFireTasks(t *testing.T) { |
| 66 t.Parallel() |
| 67 |
| 68 Convey("fireTasks works as expected", t, func() { |
| 69 ctx := memory.Use(context.Background()) |
| 70 ctx, _ = testclock.UseTime(ctx, testclock.TestTimeUTC) |
| 71 cfg := GetConfig(ctx) |
| 72 tq := taskqueue.Get(ctx) |
| 73 |
| 74 tq.Testable().CreateQueue(cfg.Name) |
| 75 |
| 76 Convey("empty", func() { |
| 77 So(fireTasks(ctx, nil), ShouldBeTrue) |
| 78 So(len(tq.Testable().GetScheduledTasks()[cfg.Name]), Sho
uldEqual, 0) |
| 79 }) |
| 80 |
| 81 Convey("basic", func() { |
| 82 So(fireTasks(ctx, map[uint64]struct{}{2: {}, 7: {}}), Sh
ouldBeTrue) |
| 83 q := tq.Testable().GetScheduledTasks()[cfg.Name] |
| 84 So(q["-62132730888_2"], ShouldResemble, &taskqueue.Task{ |
| 85 Name: "-62132730888_2", |
| 86 Method: "POST", |
| 87 Path: cfg.ProcessURL(time.Unix(-62132730888, 0
), 2), |
| 88 ETA: testclock.TestTimeUTC.Add(6 * time.Secon
d).Round(time.Second), |
| 89 }) |
| 90 So(q["-62132730888_7"], ShouldResemble, &taskqueue.Task{ |
| 91 Name: "-62132730888_7", |
| 92 Method: "POST", |
| 93 Path: cfg.ProcessURL(time.Unix(-62132730888, 0
), 7), |
| 94 ETA: testclock.TestTimeUTC.Add(6 * time.Secon
d).Round(time.Second), |
| 95 }) |
| 96 }) |
| 97 }) |
| 98 } |
OLD | NEW |