-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5ba5a9
commit bb315cf
Showing
9 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ jobs: | |
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'go.mod' | ||
- uses: brokeyourbike/[email protected] | ||
with: | ||
mockery-version: '2.36.0' | ||
- run: mockery --all --inpackage --quiet | ||
- run: go build -v ./... | ||
- run: go test -race -covermode=atomic -coverprofile=coverage.out -v ./... | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
.DS_Store | ||
.DS_Store | ||
mock_*.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/glocurrency/commons/logger" | ||
"github.com/glocurrency/commons/q" | ||
) | ||
|
||
const PubSubMessageCtx = "pubSubMessageCtx" | ||
|
||
type Locker interface { | ||
TryToLock(ctx context.Context, key string) error | ||
} | ||
|
||
type PubSubCtx struct { | ||
locker Locker | ||
} | ||
|
||
func NewPubSubCtx(l Locker) *PubSubCtx { | ||
return &PubSubCtx{locker: l} | ||
} | ||
|
||
func (m *PubSubCtx) Middleware() gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
var msg q.PubSubMessage | ||
if err := ctx.ShouldBindJSON(&msg); err != nil { | ||
logger.WithContext(ctx).WithError(err).Error("cannot unmarshal pubsub message") | ||
ctx.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
logger.WithContext(ctx).WithField("pubsub_message", msg).Debug("pubsub message received") | ||
|
||
if msg.GetUniqueKey() != "" { | ||
if err := m.locker.TryToLock(ctx, msg.GetUniqueKey()); err != nil { | ||
logger.WithContext(ctx). | ||
WithError(err). | ||
WithField("unique_key", msg.GetUniqueKey()). | ||
Error("cannot lock task") | ||
|
||
ctx.AbortWithStatus(http.StatusUnprocessableEntity) | ||
return | ||
} | ||
} | ||
|
||
ctx.Set(PubSubMessageCtx, msg) | ||
ctx.Next() | ||
} | ||
} | ||
|
||
// MustGetMessageFromContext returns the PubSub message from the context. | ||
func MustGetMessageFromContext(ctx *gin.Context) q.PubSubMessage { | ||
msg := ctx.MustGet(PubSubMessageCtx).(q.PubSubMessage) | ||
return msg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/glocurrency/commons/middleware" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//go:embed testdata/pubsub-valid.json | ||
var validPubSubMsg []byte | ||
|
||
//go:embed testdata/pubsub-valid-unique.json | ||
var validPubSubUniqueMsg []byte | ||
|
||
//go:embed testdata/pubsub-invalid.json | ||
var invalidPubSubMsg []byte | ||
|
||
func TestPubSubCtx(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
body []byte | ||
wantStatus int | ||
setupMock func(lockerMock *middleware.MockLocker) | ||
}{ | ||
{ | ||
"success without lock", | ||
validPubSubMsg, | ||
http.StatusOK, | ||
nil, | ||
}, | ||
{ | ||
"success with lock", | ||
validPubSubUniqueMsg, | ||
http.StatusOK, | ||
func(lockerMock *middleware.MockLocker) { | ||
lockerMock.On("TryToLock", mock.Anything, "unique-456").Return(nil) | ||
}, | ||
}, | ||
{ | ||
"invalid message", | ||
invalidPubSubMsg, | ||
http.StatusBadRequest, | ||
nil, | ||
}, | ||
{ | ||
"cannot lock by unique the key", | ||
validPubSubUniqueMsg, | ||
http.StatusUnprocessableEntity, | ||
func(lockerMock *middleware.MockLocker) { | ||
lockerMock.On("TryToLock", mock.Anything, "unique-456").Return(errors.New("I am an error")) | ||
}, | ||
}, | ||
} | ||
|
||
for i := range tests { | ||
test := tests[i] | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tasksMock := middleware.NewMockLocker(t) | ||
if test.setupMock != nil { | ||
test.setupMock(tasksMock) | ||
} | ||
|
||
m := middleware.NewPubSubCtx(tasksMock) | ||
req := httptest.NewRequest(http.MethodPost, "/", nil) | ||
|
||
if test.body != nil { | ||
req.Body = io.NopCloser(bytes.NewReader(test.body)) | ||
} | ||
|
||
w := httptest.NewRecorder() | ||
router := gin.New() | ||
router.Use(m.Middleware()) | ||
router.POST("/", func(ctx *gin.Context) { | ||
msg := middleware.MustGetMessageFromContext(ctx) | ||
assert.NotNil(t, msg) | ||
|
||
ctx.String(http.StatusOK, "the end.") | ||
}) | ||
router.ServeHTTP(w, req) | ||
|
||
require.Equal(t, test.wantStatus, w.Code) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"message": "I am not a valid pubsub payload" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"message": { | ||
"messageId": "158b5c5d-ce6b-4020-b822-ceb0e43b754a", | ||
"publishTime": "2023-07-09T17:49:48.087822341Z", | ||
"attributes": { | ||
"uniqueKey": "unique-456" | ||
} | ||
}, | ||
"subscription": "sub-123" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"message": { | ||
"messageId": "529ebc2c-99c6-47b0-9bf6-a92c17c8d35a", | ||
"publishTime": "2023-07-09T17:47:54.896272657Z" | ||
}, | ||
"subscription": "sub-123" | ||
} |