Skip to content

Commit 7a932d0

Browse files
author
rcorniere
committed
Added missing tests
1 parent eff622d commit 7a932d0

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

stanza/fifo_queue.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ package stanza
33
// FIFO queue for string contents
44
// Implementations have no guarantee regarding thread safety !
55
type FifoQueue interface {
6-
// Pop returns the first inserted element still in queue and delete it from queue
6+
// Pop returns the first inserted element still in queue and deletes it from queue. If queue is empty, returns nil
77
// No guarantee regarding thread safety !
88
Pop() Queueable
99

10-
// PopN returns the N first inserted elements still in queue and delete them from queue
10+
// PopN returns the N first inserted elements still in queue and deletes them from queue. If queue is empty or i<=0, returns nil
11+
// If number to pop is greater than queue length, returns all queue elements
1112
// No guarantee regarding thread safety !
1213
PopN(i int) []Queueable
1314

14-
// Peek returns a copy of the first inserted element in queue without deleting it
15+
// Peek returns a copy of the first inserted element in queue without deleting it. If queue is empty, returns nil
1516
// No guarantee regarding thread safety !
1617
Peek() Queueable
1718

18-
// Peek returns a copy of the first inserted element in queue without deleting it
19+
// Peek returns a copy of the first inserted element in queue without deleting it. If queue is empty or i<=0, returns nil.
20+
// If number to peek is greater than queue length, returns all queue elements
1921
// No guarantee regarding thread safety !
2022
PeekN() []Queueable
2123
// Push adds an element to the queue

stanza/stream_management.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ func (u *UnAckedStz) QueueableName() string {
5151
}
5252

5353
func (uaq *UnAckQueue) PeekN(n int) []Queueable {
54+
if uaq == nil {
55+
return nil
56+
}
5457
if n <= 0 {
55-
return []Queueable{}
58+
return nil
5659
}
5760
if len(uaq.Uslice) < n {
5861
n = len(uaq.Uslice)
5962
}
6063

6164
if len(uaq.Uslice) == 0 {
62-
return []Queueable{}
65+
return nil
6366
}
6467
var r []Queueable
6568
for i := 0; i < n; i++ {
@@ -70,6 +73,9 @@ func (uaq *UnAckQueue) PeekN(n int) []Queueable {
7073

7174
// No guarantee regarding thread safety !
7275
func (uaq *UnAckQueue) Pop() Queueable {
76+
if uaq == nil {
77+
return nil
78+
}
7379
r := uaq.Peek()
7480
if r != nil {
7581
uaq.Uslice = uaq.Uslice[1:]
@@ -79,12 +85,18 @@ func (uaq *UnAckQueue) Pop() Queueable {
7985

8086
// No guarantee regarding thread safety !
8187
func (uaq *UnAckQueue) PopN(n int) []Queueable {
88+
if uaq == nil {
89+
return nil
90+
}
8291
r := uaq.PeekN(n)
8392
uaq.Uslice = uaq.Uslice[len(r):]
8493
return r
8594
}
8695

8796
func (uaq *UnAckQueue) Peek() Queueable {
97+
if uaq == nil {
98+
return nil
99+
}
88100
if len(uaq.Uslice) == 0 {
89101
return nil
90102
}
@@ -93,6 +105,9 @@ func (uaq *UnAckQueue) Peek() Queueable {
93105
}
94106

95107
func (uaq *UnAckQueue) Push(s Queueable) error {
108+
if uaq == nil {
109+
return nil
110+
}
96111
pushIdx := 1
97112
if len(uaq.Uslice) != 0 {
98113
pushIdx = uaq.Uslice[len(uaq.Uslice)-1].Id + 1
@@ -114,6 +129,9 @@ func (uaq *UnAckQueue) Push(s Queueable) error {
114129
}
115130

116131
func (uaq *UnAckQueue) Empty() bool {
132+
if uaq == nil {
133+
return true
134+
}
117135
r := len(uaq.Uslice)
118136
return r == 0
119137
}
@@ -151,7 +169,7 @@ func (SMResumed) Name() string {
151169
return "Stream Management: resumed"
152170
}
153171

154-
// Resumed as defined in Stream Management spec
172+
// Resume as defined in Stream Management spec
155173
// Reference: https://xmpp.org/extensions/xep-0198.html#acking
156174
type SMResume struct {
157175
XMLName xml.Name `xml:"urn:xmpp:sm:3 resume"`

stanza/stream_management_test.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import (
88
"time"
99
)
1010

11-
// TODO : tests to add
12-
// - Pop on nil or empty slice
13-
// - PeekN (normal and too long)
11+
func TestPopEmptyQueue(t *testing.T) {
12+
var uaq stanza.UnAckQueue
13+
popped := uaq.Pop()
14+
if popped != nil {
15+
t.Fatalf("queue is empty but something was popped !")
16+
}
17+
}
1418

1519
func TestPushUnack(t *testing.T) {
1620
uaq := initUnAckQueue()
@@ -78,6 +82,41 @@ func TestPeekUnack(t *testing.T) {
7882

7983
}
8084

85+
func TestPeekNUnack(t *testing.T) {
86+
uaq := initUnAckQueue()
87+
initLen := len(uaq.Uslice)
88+
randPop := rand.Int31n(int32(initLen))
89+
90+
peeked := uaq.PeekN(int(randPop))
91+
92+
if len(uaq.Uslice) != initLen {
93+
t.Fatalf("queue length changed whith peek n operation : had %d found %d after peek", initLen, len(uaq.Uslice))
94+
}
95+
96+
if len(peeked) != int(randPop) {
97+
t.Fatalf("did not peek the correct number of element from queue. Expected %d got %d", randPop, len(peeked))
98+
}
99+
}
100+
101+
func TestPeekNUnackTooLong(t *testing.T) {
102+
uaq := initUnAckQueue()
103+
initLen := len(uaq.Uslice)
104+
105+
// Have a random number of elements to peek that's greater than the queue size
106+
randPop := rand.Int31n(int32(initLen)) + 1 + int32(initLen)
107+
108+
peeked := uaq.PeekN(int(randPop))
109+
110+
if len(uaq.Uslice) != initLen {
111+
t.Fatalf("total length changed whith peek n operation : had %d found %d after pop", initLen, len(uaq.Uslice))
112+
}
113+
114+
if len(peeked) != initLen {
115+
t.Fatalf("did not peek the correct number of element from queue. Expected %d got %d", initLen, len(peeked))
116+
}
117+
118+
}
119+
81120
func TestPopNUnack(t *testing.T) {
82121
uaq := initUnAckQueue()
83122
initLen := len(uaq.Uslice)

0 commit comments

Comments
 (0)