serverlock_test.go 1.4 KB

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