-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
1 changed file
with
82 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package queue | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/uozi-tech/cosy/redis" | ||
"github.com/uozi-tech/cosy/settings" | ||
"testing" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
settings.Init("../app.ini") | ||
redis.Init() | ||
|
||
t.Run("test New", queueNew) | ||
t.Run("test ProduceConsume", produceConsume) | ||
t.Run("test LockUnlock", lockUnlock) | ||
t.Run("test Len", testLen) | ||
t.Run("test ProduceConsumeWithRetry", testProduceConsumeWithRetry) | ||
} | ||
|
||
func queueNew(t *testing.T) { | ||
q := New[string]("testQueue", LeftToRight) | ||
assert.Equal(t, "testQueue", q.name) | ||
assert.Equal(t, LeftToRight, q.direction) | ||
assert.Equal(t, "testQueue__left", q.listName) | ||
} | ||
|
||
func produceConsume(t *testing.T) { | ||
q := New[string]("testQueue", LeftToRight) | ||
data := "testData" | ||
|
||
err := q.Produce(&data) | ||
assert.NoError(t, err) | ||
|
||
var result string | ||
err = q.Consume(&result) | ||
assert.NoError(t, err) | ||
assert.Equal(t, data, result) | ||
} | ||
|
||
func lockUnlock(t *testing.T) { | ||
q := New[string]("testQueue", LeftToRight) | ||
|
||
err := q.Lock() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, q.lock) | ||
|
||
err = q.Unlock() | ||
assert.NoError(t, err) | ||
assert.Nil(t, q.lock) | ||
} | ||
|
||
func testLen(t *testing.T) { | ||
q := New[string]("testQueue", LeftToRight) | ||
data := "testData" | ||
|
||
err := q.Produce(&data) | ||
assert.NoError(t, err) | ||
|
||
length := q.Len() | ||
assert.Equal(t, int64(1), length) | ||
} | ||
|
||
func testProduceConsumeWithRetry(t *testing.T) { | ||
qInt := New[int]("testQueue", LeftToRight) | ||
qStr := New[string]("testQueue", LeftToRight) | ||
data := "testData" | ||
|
||
err := qStr.Produce(&data) | ||
assert.NoError(t, err) | ||
|
||
// Simulate a failure in unmarshalling | ||
var result int | ||
err = qInt.Consume(&result) | ||
assert.Error(t, err) | ||
|
||
// Ensure the data is still in the queue | ||
var retryResult string | ||
err = qStr.Consume(&retryResult) | ||
assert.NoError(t, err) | ||
assert.Equal(t, data, retryResult) | ||
} |