serverlock_integration_test.go 793 B

1234567891011121314151617181920212223242526272829303132333435
  1. // +build integration
  2. package serverlock
  3. import (
  4. "context"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestServerLok(t *testing.T) {
  10. sl := createTestableServerLock(t)
  11. counter := 0
  12. fn := func() { counter++ }
  13. atInterval := time.Second * 1
  14. ctx := context.Background()
  15. //this time `fn` should be executed
  16. assert.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
  17. //this should not execute `fn`
  18. assert.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
  19. assert.Nil(t, sl.LockAndExecute(ctx, "test-operation", atInterval, fn))
  20. // wait 2 second.
  21. <-time.After(time.Second * 2)
  22. // now `fn` should be executed again
  23. err := sl.LockAndExecute(ctx, "test-operation", atInterval, fn)
  24. assert.Nil(t, err)
  25. assert.Equal(t, counter, 2)
  26. }