|
|
|
|
@ -13,63 +13,28 @@ import ( |
|
|
|
|
func TestServerLok(t *testing.T) { |
|
|
|
|
sl := createTestableServerLock(t) |
|
|
|
|
|
|
|
|
|
Convey("Server lock integration test", t, func() { |
|
|
|
|
|
|
|
|
|
Convey("Check that we can call OncePerServerGroup multiple times without executing callback", func() { |
|
|
|
|
counter := 0 |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
//this time `fn` should be executed
|
|
|
|
|
err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ }) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
//this should not execute `fn`
|
|
|
|
|
err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ }) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
//this should not execute `fn`
|
|
|
|
|
err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ }) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
// wg := sync.WaitGroup{}
|
|
|
|
|
// for i := 0; i < 3; i++ {
|
|
|
|
|
// wg.Add(1)
|
|
|
|
|
// go func(index int) {
|
|
|
|
|
// defer wg.Done()
|
|
|
|
|
// //sl := createTestableServerLock(t)
|
|
|
|
|
// //<-time.After(time.Second)
|
|
|
|
|
|
|
|
|
|
// j := 0
|
|
|
|
|
// for {
|
|
|
|
|
// select {
|
|
|
|
|
// case <-time.Tick(time.Second):
|
|
|
|
|
// fmt.Printf("running worker %d loop %d\n", index, j)
|
|
|
|
|
// err := sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*2, func() {
|
|
|
|
|
// counter++
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// t.Errorf("expected. err: %v", err)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// j++
|
|
|
|
|
// if j > 3 {
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }(i)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// wg.Wait()
|
|
|
|
|
|
|
|
|
|
// wait 5 second.
|
|
|
|
|
<-time.After(time.Second * 10) |
|
|
|
|
|
|
|
|
|
// now `fn` should be executed again
|
|
|
|
|
err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ }) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
So(counter, ShouldEqual, 2) |
|
|
|
|
}) |
|
|
|
|
Convey("Server lock integration tests", t, func() { |
|
|
|
|
counter := 0 |
|
|
|
|
var err error |
|
|
|
|
incCounter := func() { counter++ } |
|
|
|
|
atInterval := time.Second * 1 |
|
|
|
|
ctx := context.Background() |
|
|
|
|
|
|
|
|
|
//this time `fn` should be executed
|
|
|
|
|
So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) |
|
|
|
|
|
|
|
|
|
//this should not execute `fn`
|
|
|
|
|
So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) |
|
|
|
|
So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) |
|
|
|
|
So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) |
|
|
|
|
So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil) |
|
|
|
|
|
|
|
|
|
// wait 5 second.
|
|
|
|
|
<-time.After(atInterval * 2) |
|
|
|
|
|
|
|
|
|
// now `fn` should be executed again
|
|
|
|
|
err = sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
So(counter, ShouldEqual, 2) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|