Added missing tests

160-regression
rcorniere 4 years ago
parent eff622df76
commit 7a932d0504

@ -3,19 +3,21 @@ package stanza
// FIFO queue for string contents // FIFO queue for string contents
// Implementations have no guarantee regarding thread safety ! // Implementations have no guarantee regarding thread safety !
type FifoQueue interface { type FifoQueue interface {
// Pop returns the first inserted element still in queue and delete it from queue // Pop returns the first inserted element still in queue and deletes it from queue. If queue is empty, returns nil
// No guarantee regarding thread safety ! // No guarantee regarding thread safety !
Pop() Queueable Pop() Queueable
// PopN returns the N first inserted elements still in queue and delete them from queue // PopN returns the N first inserted elements still in queue and deletes them from queue. If queue is empty or i<=0, returns nil
// If number to pop is greater than queue length, returns all queue elements
// No guarantee regarding thread safety ! // No guarantee regarding thread safety !
PopN(i int) []Queueable PopN(i int) []Queueable
// Peek returns a copy of the first inserted element in queue without deleting it // Peek returns a copy of the first inserted element in queue without deleting it. If queue is empty, returns nil
// No guarantee regarding thread safety ! // No guarantee regarding thread safety !
Peek() Queueable Peek() Queueable
// Peek returns a copy of the first inserted element in queue without deleting it // Peek returns a copy of the first inserted element in queue without deleting it. If queue is empty or i<=0, returns nil.
// If number to peek is greater than queue length, returns all queue elements
// No guarantee regarding thread safety ! // No guarantee regarding thread safety !
PeekN() []Queueable PeekN() []Queueable
// Push adds an element to the queue // Push adds an element to the queue

@ -51,15 +51,18 @@ func (u *UnAckedStz) QueueableName() string {
} }
func (uaq *UnAckQueue) PeekN(n int) []Queueable { func (uaq *UnAckQueue) PeekN(n int) []Queueable {
if uaq == nil {
return nil
}
if n <= 0 { if n <= 0 {
return []Queueable{} return nil
} }
if len(uaq.Uslice) < n { if len(uaq.Uslice) < n {
n = len(uaq.Uslice) n = len(uaq.Uslice)
} }
if len(uaq.Uslice) == 0 { if len(uaq.Uslice) == 0 {
return []Queueable{} return nil
} }
var r []Queueable var r []Queueable
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -70,6 +73,9 @@ func (uaq *UnAckQueue) PeekN(n int) []Queueable {
// No guarantee regarding thread safety ! // No guarantee regarding thread safety !
func (uaq *UnAckQueue) Pop() Queueable { func (uaq *UnAckQueue) Pop() Queueable {
if uaq == nil {
return nil
}
r := uaq.Peek() r := uaq.Peek()
if r != nil { if r != nil {
uaq.Uslice = uaq.Uslice[1:] uaq.Uslice = uaq.Uslice[1:]
@ -79,12 +85,18 @@ func (uaq *UnAckQueue) Pop() Queueable {
// No guarantee regarding thread safety ! // No guarantee regarding thread safety !
func (uaq *UnAckQueue) PopN(n int) []Queueable { func (uaq *UnAckQueue) PopN(n int) []Queueable {
if uaq == nil {
return nil
}
r := uaq.PeekN(n) r := uaq.PeekN(n)
uaq.Uslice = uaq.Uslice[len(r):] uaq.Uslice = uaq.Uslice[len(r):]
return r return r
} }
func (uaq *UnAckQueue) Peek() Queueable { func (uaq *UnAckQueue) Peek() Queueable {
if uaq == nil {
return nil
}
if len(uaq.Uslice) == 0 { if len(uaq.Uslice) == 0 {
return nil return nil
} }
@ -93,6 +105,9 @@ func (uaq *UnAckQueue) Peek() Queueable {
} }
func (uaq *UnAckQueue) Push(s Queueable) error { func (uaq *UnAckQueue) Push(s Queueable) error {
if uaq == nil {
return nil
}
pushIdx := 1 pushIdx := 1
if len(uaq.Uslice) != 0 { if len(uaq.Uslice) != 0 {
pushIdx = uaq.Uslice[len(uaq.Uslice)-1].Id + 1 pushIdx = uaq.Uslice[len(uaq.Uslice)-1].Id + 1
@ -114,6 +129,9 @@ func (uaq *UnAckQueue) Push(s Queueable) error {
} }
func (uaq *UnAckQueue) Empty() bool { func (uaq *UnAckQueue) Empty() bool {
if uaq == nil {
return true
}
r := len(uaq.Uslice) r := len(uaq.Uslice)
return r == 0 return r == 0
} }
@ -151,7 +169,7 @@ func (SMResumed) Name() string {
return "Stream Management: resumed" return "Stream Management: resumed"
} }
// Resumed as defined in Stream Management spec // Resume as defined in Stream Management spec
// Reference: https://xmpp.org/extensions/xep-0198.html#acking // Reference: https://xmpp.org/extensions/xep-0198.html#acking
type SMResume struct { type SMResume struct {
XMLName xml.Name `xml:"urn:xmpp:sm:3 resume"` XMLName xml.Name `xml:"urn:xmpp:sm:3 resume"`

@ -8,9 +8,13 @@ import (
"time" "time"
) )
// TODO : tests to add func TestPopEmptyQueue(t *testing.T) {
// - Pop on nil or empty slice var uaq stanza.UnAckQueue
// - PeekN (normal and too long) popped := uaq.Pop()
if popped != nil {
t.Fatalf("queue is empty but something was popped !")
}
}
func TestPushUnack(t *testing.T) { func TestPushUnack(t *testing.T) {
uaq := initUnAckQueue() uaq := initUnAckQueue()
@ -78,6 +82,41 @@ func TestPeekUnack(t *testing.T) {
} }
func TestPeekNUnack(t *testing.T) {
uaq := initUnAckQueue()
initLen := len(uaq.Uslice)
randPop := rand.Int31n(int32(initLen))
peeked := uaq.PeekN(int(randPop))
if len(uaq.Uslice) != initLen {
t.Fatalf("queue length changed whith peek n operation : had %d found %d after peek", initLen, len(uaq.Uslice))
}
if len(peeked) != int(randPop) {
t.Fatalf("did not peek the correct number of element from queue. Expected %d got %d", randPop, len(peeked))
}
}
func TestPeekNUnackTooLong(t *testing.T) {
uaq := initUnAckQueue()
initLen := len(uaq.Uslice)
// Have a random number of elements to peek that's greater than the queue size
randPop := rand.Int31n(int32(initLen)) + 1 + int32(initLen)
peeked := uaq.PeekN(int(randPop))
if len(uaq.Uslice) != initLen {
t.Fatalf("total length changed whith peek n operation : had %d found %d after pop", initLen, len(uaq.Uslice))
}
if len(peeked) != initLen {
t.Fatalf("did not peek the correct number of element from queue. Expected %d got %d", initLen, len(peeked))
}
}
func TestPopNUnack(t *testing.T) { func TestPopNUnack(t *testing.T) {
uaq := initUnAckQueue() uaq := initUnAckQueue()
initLen := len(uaq.Uslice) initLen := len(uaq.Uslice)

Loading…
Cancel
Save