serverlock_integration_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 tests", t, func() {
  12. counter := 0
  13. var err error
  14. incCounter := func() { counter++ }
  15. atInterval := time.Second * 1
  16. ctx := context.Background()
  17. //this time `fn` should be executed
  18. So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
  19. //this should not execute `fn`
  20. So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
  21. So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
  22. So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
  23. So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
  24. // wait 5 second.
  25. <-time.After(atInterval * 2)
  26. // now `fn` should be executed again
  27. err = sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter)
  28. So(err, ShouldBeNil)
  29. So(counter, ShouldEqual, 2)
  30. })
  31. }