serverlock_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package serverlock
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/services/sqlstore"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func createTestableServerLock(t *testing.T) *ServerLockService {
  11. t.Helper()
  12. sqlstore := sqlstore.InitTestDB(t)
  13. return &ServerLockService{
  14. SQLStore: sqlstore,
  15. log: log.New("test-logger"),
  16. }
  17. }
  18. func TestServerLock(t *testing.T) {
  19. Convey("Server lock", t, func() {
  20. sl := createTestableServerLock(t)
  21. operationUID := "test-operation"
  22. first, err := sl.getOrCreate(context.Background(), operationUID)
  23. So(err, ShouldBeNil)
  24. lastExecution := first.LastExecution
  25. Convey("trying to create three new row locks", func() {
  26. for i := 0; i < 3; i++ {
  27. first, err = sl.getOrCreate(context.Background(), operationUID)
  28. So(err, ShouldBeNil)
  29. So(first.OperationUid, ShouldEqual, operationUID)
  30. So(first.Id, ShouldEqual, 1)
  31. }
  32. Convey("Should not create new since lock already exist", func() {
  33. So(lastExecution, ShouldEqual, first.LastExecution)
  34. })
  35. })
  36. Convey("Should be able to create lock on first row", func() {
  37. gotLock, err := sl.acquireLock(context.Background(), first, time.Second*1)
  38. So(err, ShouldBeNil)
  39. So(gotLock, ShouldBeTrue)
  40. gotLock, err = sl.acquireLock(context.Background(), first, time.Second*1)
  41. So(err, ShouldBeNil)
  42. So(gotLock, ShouldBeFalse)
  43. })
  44. })
  45. }