serverlock_integration_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build integration
  2. package serverlock
  3. import (
  4. "context"
  5. "testing"
  6. "time"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestServerLok(t *testing.T) {
  10. sl := createTestableServerLock(t)
  11. Convey("Server lock integration test", t, func() {
  12. Convey("Check that we can call OncePerServerGroup multiple times without executing callback", func() {
  13. counter := 0
  14. var err error
  15. //this time `fn` should be executed
  16. err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ })
  17. So(err, ShouldBeNil)
  18. //this should not execute `fn`
  19. err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ })
  20. So(err, ShouldBeNil)
  21. //this should not execute `fn`
  22. err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ })
  23. So(err, ShouldBeNil)
  24. // wg := sync.WaitGroup{}
  25. // for i := 0; i < 3; i++ {
  26. // wg.Add(1)
  27. // go func(index int) {
  28. // defer wg.Done()
  29. // //sl := createTestableServerLock(t)
  30. // //<-time.After(time.Second)
  31. // j := 0
  32. // for {
  33. // select {
  34. // case <-time.Tick(time.Second):
  35. // fmt.Printf("running worker %d loop %d\n", index, j)
  36. // err := sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*2, func() {
  37. // counter++
  38. // })
  39. // if err != nil {
  40. // t.Errorf("expected. err: %v", err)
  41. // }
  42. // j++
  43. // if j > 3 {
  44. // return
  45. // }
  46. // }
  47. // }
  48. // }(i)
  49. // }
  50. // wg.Wait()
  51. // wait 5 second.
  52. <-time.After(time.Second * 10)
  53. // now `fn` should be executed again
  54. err = sl.OncePerServerGroup(context.Background(), "test-operation", time.Second*5, func() { counter++ })
  55. So(err, ShouldBeNil)
  56. So(counter, ShouldEqual, 2)
  57. })
  58. })
  59. }