diff --git a/Dockerfile.test b/Dockerfile.test new file mode 100644 index 0000000..ba79d3c --- /dev/null +++ b/Dockerfile.test @@ -0,0 +1,30 @@ +FROM ghcr.io/aldor007/mort-base + +ENV GOLANG_VERSION 1.16.6 +ARG TARGETARCH amd64 +ARG TAG 'dev' +ARG COMMIT "master" +ARG DATE "now" + +ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-$TARGETARCH.tar.gz + +RUN curl -fsSL --insecure "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \ + && tar -C /usr/local -xzf golang.tar.gz \ + && rm golang.tar.gz + +ENV WORKDIR /workspace +ENV PATH /usr/local/go/bin:$PATH + + +WORKDIR $WORKDIR +COPY go.mod ./ +COPY go.sum ./ +RUN go mod download + +COPY cmd/ $WORKDIR/cmd +COPY .godir ${WORKDIR}/.godir +COPY configuration/ ${WORKDIR}/configuration +COPY etc/ ${WORKDIR}/etc +COPY pkg/ ${WORKDIR}/pkg +COPY scripts/ ${WORKDIR}/scripts +COPY Makefile ${WORKDIR}/Makefile diff --git a/docker-compose.yaml b/docker-compose.yaml index abb3bb5..abccea1 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,7 +15,7 @@ services: tests: build: context: . - dockerfile: Dockerfile.base + dockerfile: Dockerfile.test args: - TAG=dev - COMMIT=master diff --git a/pkg/lock/redis.go b/pkg/lock/redis.go index a84e353..7a3818d 100644 --- a/pkg/lock/redis.go +++ b/pkg/lock/redis.go @@ -85,7 +85,12 @@ func (m *RedisLock) Lock(ctx context.Context, key string) (result LockResult, ok lock.Refresh(ctx, time.Millisecond*500, nil) } else { lock, err := m.client.Obtain(ctx, key, 60*time.Second, nil) - if err != nil { + if err == redislock.ErrNotObtained { + result, ok = m.memoryLock.Lock(ctx, key) + ok = false + return + + } else if err != nil { result.Error = err return } diff --git a/pkg/lock/redis_test.go b/pkg/lock/redis_test.go index 6328a88..538a14e 100644 --- a/pkg/lock/redis_test.go +++ b/pkg/lock/redis_test.go @@ -31,6 +31,8 @@ func TestRedisLock_Lock(t *testing.T) { assert.Nil(t, c.ResponseChan, "shouldn't return channel") assert.Nil(t, c.Error, "error should be nil") + mock.ClearExpect() + mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(false) resChan, lock := l.Lock(ctx, key) assert.False(t, lock, "Shouldn't acquire lock") @@ -61,7 +63,7 @@ func TestRedisLock_NotifyAndReleaseWhenError(t *testing.T) { assert.Nil(t, c.Error, "error should be nil") mock.ClearExpect() - mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(true) + mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(false) result, lock := l.Lock(ctx, key) assert.False(t, lock, "Shouldn't acquire lock") @@ -96,7 +98,7 @@ func TestRedisLock_NotifyAndRelease(t *testing.T) { assert.Nil(t, c.ResponseChan, "shouldn't return channel") mock.ClearExpect() - mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(true) + mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(false) result, lock := l.Lock(ctx, key) assert.False(t, lock, "Shouldn't acquire lock")