-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add queue to ws subscription filters (#2769)
- Loading branch information
Showing
6 changed files
with
229 additions
and
68 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
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
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,66 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// ErrQueueEmpty is returned when a queue operation | ||
// depends on the queue to not be empty, but it is empty | ||
var ErrQueueEmpty = fmt.Errorf("queue is empty") | ||
|
||
// Queue is a generic queue implementation that implements FIFO | ||
type Queue[T any] struct { | ||
items []T | ||
mutex sync.Mutex | ||
} | ||
|
||
// NewQueue creates a new instance of queue and initializes it | ||
func NewQueue[T any]() *Queue[T] { | ||
return &Queue[T]{ | ||
items: make([]T, 0), | ||
} | ||
} | ||
|
||
// Push enqueue an item | ||
func (q *Queue[T]) Push(item T) { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
q.items = append(q.items, item) | ||
} | ||
|
||
// Top returns the top level item without removing it | ||
func (q *Queue[T]) Top() (T, error) { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
var v T | ||
if len(q.items) == 0 { | ||
return v, ErrQueueEmpty | ||
} | ||
return q.items[0], nil | ||
} | ||
|
||
// Pop returns the top level item and unqueues it | ||
func (q *Queue[T]) Pop() (T, error) { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
var v T | ||
if len(q.items) == 0 { | ||
return v, ErrQueueEmpty | ||
} | ||
v = q.items[0] | ||
q.items = q.items[1:] | ||
return v, nil | ||
} | ||
|
||
// Len returns the size of the queue | ||
func (q *Queue[T]) Len() int { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
return len(q.items) | ||
} | ||
|
||
// IsEmpty returns false if the queue has itens, otherwise true | ||
func (q *Queue[T]) IsEmpty() bool { | ||
return q.Len() == 0 | ||
} |
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,52 @@ | ||
package state | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
q := NewQueue[int]() | ||
|
||
q.Push(10) | ||
q.Push(20) | ||
q.Push(30) | ||
|
||
top, err := q.Top() | ||
require.NoError(t, err) | ||
assert.Equal(t, 10, top) | ||
assert.Equal(t, 3, q.Len()) | ||
assert.Equal(t, false, q.IsEmpty()) | ||
|
||
pop, err := q.Pop() | ||
require.NoError(t, err) | ||
assert.Equal(t, 10, pop) | ||
assert.Equal(t, 2, q.Len()) | ||
assert.Equal(t, false, q.IsEmpty()) | ||
|
||
top, err = q.Top() | ||
require.NoError(t, err) | ||
assert.Equal(t, 20, top) | ||
assert.Equal(t, 2, q.Len()) | ||
assert.Equal(t, false, q.IsEmpty()) | ||
|
||
pop, err = q.Pop() | ||
require.NoError(t, err) | ||
assert.Equal(t, 20, pop) | ||
assert.Equal(t, 1, q.Len()) | ||
assert.Equal(t, false, q.IsEmpty()) | ||
|
||
pop, err = q.Pop() | ||
require.NoError(t, err) | ||
assert.Equal(t, 30, pop) | ||
assert.Equal(t, 0, q.Len()) | ||
assert.Equal(t, true, q.IsEmpty()) | ||
|
||
_, err = q.Top() | ||
require.Error(t, ErrQueueEmpty, err) | ||
|
||
_, err = q.Pop() | ||
require.Error(t, ErrQueueEmpty, err) | ||
} |