From 7a932d0504ac4a276e3ca7be383fbb06310d7ebd Mon Sep 17 00:00:00 2001 From: rcorniere Date: Mon, 9 Mar 2020 17:12:32 +0100 Subject: [PATCH] Added missing tests --- stanza/fifo_queue.go | 10 ++++--- stanza/stream_management.go | 24 ++++++++++++++--- stanza/stream_management_test.go | 45 +++++++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/stanza/fifo_queue.go b/stanza/fifo_queue.go index dcdab02..ca28810 100644 --- a/stanza/fifo_queue.go +++ b/stanza/fifo_queue.go @@ -3,19 +3,21 @@ package stanza // FIFO queue for string contents // Implementations have no guarantee regarding thread safety ! 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 ! 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 ! 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 ! 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 ! PeekN() []Queueable // Push adds an element to the queue diff --git a/stanza/stream_management.go b/stanza/stream_management.go index f48d9ca..a2a4f0b 100644 --- a/stanza/stream_management.go +++ b/stanza/stream_management.go @@ -51,15 +51,18 @@ func (u *UnAckedStz) QueueableName() string { } func (uaq *UnAckQueue) PeekN(n int) []Queueable { + if uaq == nil { + return nil + } if n <= 0 { - return []Queueable{} + return nil } if len(uaq.Uslice) < n { n = len(uaq.Uslice) } if len(uaq.Uslice) == 0 { - return []Queueable{} + return nil } var r []Queueable for i := 0; i < n; i++ { @@ -70,6 +73,9 @@ func (uaq *UnAckQueue) PeekN(n int) []Queueable { // No guarantee regarding thread safety ! func (uaq *UnAckQueue) Pop() Queueable { + if uaq == nil { + return nil + } r := uaq.Peek() if r != nil { uaq.Uslice = uaq.Uslice[1:] @@ -79,12 +85,18 @@ func (uaq *UnAckQueue) Pop() Queueable { // No guarantee regarding thread safety ! func (uaq *UnAckQueue) PopN(n int) []Queueable { + if uaq == nil { + return nil + } r := uaq.PeekN(n) uaq.Uslice = uaq.Uslice[len(r):] return r } func (uaq *UnAckQueue) Peek() Queueable { + if uaq == nil { + return nil + } if len(uaq.Uslice) == 0 { return nil } @@ -93,6 +105,9 @@ func (uaq *UnAckQueue) Peek() Queueable { } func (uaq *UnAckQueue) Push(s Queueable) error { + if uaq == nil { + return nil + } pushIdx := 1 if len(uaq.Uslice) != 0 { 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 { + if uaq == nil { + return true + } r := len(uaq.Uslice) return r == 0 } @@ -151,7 +169,7 @@ func (SMResumed) Name() string { 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 type SMResume struct { XMLName xml.Name `xml:"urn:xmpp:sm:3 resume"` diff --git a/stanza/stream_management_test.go b/stanza/stream_management_test.go index 8f51ba0..1b3443e 100644 --- a/stanza/stream_management_test.go +++ b/stanza/stream_management_test.go @@ -8,9 +8,13 @@ import ( "time" ) -// TODO : tests to add -// - Pop on nil or empty slice -// - PeekN (normal and too long) +func TestPopEmptyQueue(t *testing.T) { + var uaq stanza.UnAckQueue + popped := uaq.Pop() + if popped != nil { + t.Fatalf("queue is empty but something was popped !") + } +} func TestPushUnack(t *testing.T) { 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) { uaq := initUnAckQueue() initLen := len(uaq.Uslice)