diff options
author | George Goldberg <george@gberg.me> | 2017-05-09 14:13:24 +0100 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-05-09 08:13:24 -0500 |
commit | 622998add12734a6c2b5d79918338a4d6dca7ce6 (patch) | |
tree | 198d507eb04c5684d5b191b17ed957f02f2df6ed /vendor/gopkg.in/olivere/elastic.v5/backoff_test.go | |
parent | b25021b9129820147bf596b834d438ef218acf28 (diff) | |
download | chat-622998add12734a6c2b5d79918338a4d6dca7ce6.tar.gz chat-622998add12734a6c2b5d79918338a4d6dca7ce6.tar.bz2 chat-622998add12734a6c2b5d79918338a4d6dca7ce6.zip |
PLT-6398: Add dependency on go elastic search library. (#6340)
Diffstat (limited to 'vendor/gopkg.in/olivere/elastic.v5/backoff_test.go')
-rw-r--r-- | vendor/gopkg.in/olivere/elastic.v5/backoff_test.go | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/vendor/gopkg.in/olivere/elastic.v5/backoff_test.go b/vendor/gopkg.in/olivere/elastic.v5/backoff_test.go new file mode 100644 index 000000000..aba7dcf76 --- /dev/null +++ b/vendor/gopkg.in/olivere/elastic.v5/backoff_test.go @@ -0,0 +1,142 @@ +package elastic + +import ( + "math/rand" + "testing" + "time" +) + +func TestZeroBackoff(t *testing.T) { + b := ZeroBackoff{} + _, ok := b.Next(0) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } +} + +func TestStopBackoff(t *testing.T) { + b := StopBackoff{} + _, ok := b.Next(0) + if ok { + t.Fatalf("expected %v, got %v", false, ok) + } +} + +func TestConstantBackoff(t *testing.T) { + b := NewConstantBackoff(time.Second) + d, ok := b.Next(0) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } + if d != time.Second { + t.Fatalf("expected %v, got %v", time.Second, d) + } +} + +func TestSimpleBackoff(t *testing.T) { + var tests = []struct { + Duration time.Duration + Continue bool + }{ + // #0 + { + Duration: 1 * time.Millisecond, + Continue: true, + }, + // #1 + { + Duration: 2 * time.Millisecond, + Continue: true, + }, + // #2 + { + Duration: 7 * time.Millisecond, + Continue: true, + }, + // #3 + { + Duration: 0, + Continue: false, + }, + // #4 + { + Duration: 0, + Continue: false, + }, + } + + b := NewSimpleBackoff(1, 2, 7) + + for i, tt := range tests { + d, ok := b.Next(i) + if got, want := ok, tt.Continue; got != want { + t.Fatalf("#%d: expected %v, got %v", i, want, got) + } + if got, want := d, tt.Duration; got != want { + t.Fatalf("#%d: expected %v, got %v", i, want, got) + } + } +} + +func TestExponentialBackoff(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + + min := time.Duration(8) * time.Millisecond + max := time.Duration(256) * time.Millisecond + b := NewExponentialBackoff(min, max) + + between := func(value time.Duration, a, b int) bool { + x := int(value / time.Millisecond) + return a <= x && x <= b + } + + got, ok := b.Next(0) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } + if !between(got, 8, 256) { + t.Errorf("expected [%v..%v], got %v", 8, 256, got) + } + + got, ok = b.Next(1) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } + if !between(got, 8, 256) { + t.Errorf("expected [%v..%v], got %v", 8, 256, got) + } + + got, ok = b.Next(2) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } + if !between(got, 8, 256) { + t.Errorf("expected [%v..%v], got %v", 8, 256, got) + } + + got, ok = b.Next(3) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } + if !between(got, 8, 256) { + t.Errorf("expected [%v..%v], got %v", 8, 256, got) + } + + got, ok = b.Next(4) + if !ok { + t.Fatalf("expected %v, got %v", true, ok) + } + if !between(got, 8, 256) { + t.Errorf("expected [%v..%v], got %v", 8, 256, got) + } + + got, ok = b.Next(5) + if ok { + t.Fatalf("expected %v, got %v", false, ok) + } + + got, ok = b.Next(6) + if ok { + t.Fatalf("expected %v, got %v", false, ok) + } +} |