192 lines
3.5 KiB
Go
192 lines
3.5 KiB
Go
package connection
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var (
|
|
message1 = "message-1"
|
|
message2 = "message-2"
|
|
message3 = "message-3"
|
|
message4 = "message-4"
|
|
message5 = "message-5"
|
|
message6 = "message-6"
|
|
message7 = "message-7"
|
|
)
|
|
|
|
func Test_MessageBuffer_Add(t *testing.T) {
|
|
buf := newMessageBuffer(3)
|
|
|
|
t.Run("insert without wrapping", func(t *testing.T) {
|
|
buf.Insert("message-1")
|
|
assert.Equal(t, 1, buf.insertIndex)
|
|
|
|
buf.Insert("message-2")
|
|
assert.Equal(t, 2, buf.insertIndex)
|
|
|
|
buf.Insert("message-3")
|
|
assert.Equal(
|
|
t,
|
|
[]message{
|
|
{content: message1, new: true},
|
|
{content: message2, new: true},
|
|
{content: message3, new: true},
|
|
},
|
|
buf.messages,
|
|
)
|
|
})
|
|
|
|
t.Run("insert that causes wrapping", func(t *testing.T) {
|
|
buf.Insert("message-4")
|
|
assert.Equal(
|
|
t,
|
|
[]message{
|
|
{content: message4, new: true},
|
|
{content: message2, new: true},
|
|
{content: message3, new: true},
|
|
},
|
|
buf.messages)
|
|
})
|
|
}
|
|
|
|
func Test_MessageBuffer_GetWaitsForFirstData(t *testing.T) {
|
|
waitTime := 50 * time.Millisecond
|
|
buf := newMessageBuffer(1)
|
|
timer := time.NewTimer(waitTime)
|
|
startTime := time.Now()
|
|
|
|
go func() {
|
|
<-timer.C
|
|
buf.Insert("delayed-message")
|
|
}()
|
|
|
|
msg := buf.Get()
|
|
|
|
endTime := time.Now()
|
|
|
|
assert.Equal(t, "delayed-message", msg)
|
|
assert.GreaterOrEqual(t, endTime.Sub(startTime), waitTime)
|
|
}
|
|
|
|
func Test_MessageBuffer_GetWaitsForNewData(t *testing.T) {
|
|
buf := newMessageBuffer(2)
|
|
|
|
buf.Insert("message-1")
|
|
msg := buf.Get()
|
|
assert.Equal(t, "message-1", msg)
|
|
|
|
go func() {
|
|
timer := time.NewTimer(100 * time.Millisecond)
|
|
<-timer.C
|
|
buf.Insert("delayed-message")
|
|
}()
|
|
|
|
msg = buf.Get()
|
|
assert.Equal(t, "delayed-message", msg)
|
|
}
|
|
|
|
func Test_MessageBuffer_IndexesAreCorrectAfterOverwritingOldData(t *testing.T) {
|
|
buf := newMessageBuffer(2)
|
|
|
|
buf.Insert("message-1")
|
|
buf.Insert("message-2")
|
|
|
|
assert.Equal(
|
|
t,
|
|
[]message{
|
|
{content: message1, new: true},
|
|
{content: message2, new: true},
|
|
},
|
|
buf.messages)
|
|
|
|
buf.Insert("message-3")
|
|
assert.Equal(
|
|
t,
|
|
[]message{
|
|
{content: message3, new: true},
|
|
{content: message2, new: true},
|
|
},
|
|
buf.messages)
|
|
|
|
msg := buf.Get()
|
|
assert.Equal(t, "message-2", msg)
|
|
}
|
|
|
|
func Test_MessageBuffer_GetWaitsForNewDataIfOldOneWasAlreadyGotten(t *testing.T) {
|
|
buf := newMessageBuffer(2)
|
|
|
|
buf.Insert(message1)
|
|
msg := buf.Get()
|
|
assert.Equal(t, message1, msg)
|
|
|
|
buf.Insert(message2)
|
|
msg = buf.Get()
|
|
assert.Equal(t, message2, msg)
|
|
|
|
go func() {
|
|
<-time.NewTimer(100 * time.Millisecond).C
|
|
buf.Insert(message3)
|
|
}()
|
|
|
|
msg = buf.Get()
|
|
assert.Equal(t, message3, msg)
|
|
|
|
}
|
|
|
|
func Test_MessageBuffer_InsertCatchesUpWithRead(t *testing.T) {
|
|
buf := newMessageBuffer(5)
|
|
|
|
buf.Insert(message1)
|
|
buf.Insert(message2)
|
|
buf.Insert(message3)
|
|
buf.Insert(message4)
|
|
buf.Insert(message5)
|
|
buf.Insert(message6)
|
|
buf.Insert(message7)
|
|
|
|
msg := buf.Get()
|
|
|
|
assert.Equal(t, message3, msg)
|
|
}
|
|
|
|
func Test_MessageBuffer_FuckShitUp(t *testing.T) {
|
|
size := 10
|
|
buf := newMessageBuffer(size)
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
var readMsg = make([]string, 0)
|
|
go func() {
|
|
for i := 0; i < size*10; i++ {
|
|
msg := buf.Get()
|
|
if msg == "99" {
|
|
break
|
|
}
|
|
fmt.Println("i = ", i, ": msg = ", msg)
|
|
readMsg = append(readMsg, msg)
|
|
}
|
|
wg.Done()
|
|
}()
|
|
|
|
go func() {
|
|
for i := 0; i < size*10; i++ {
|
|
if i%10 == 0 {
|
|
timer := time.NewTimer(1 * time.Millisecond)
|
|
<-timer.C
|
|
}
|
|
buf.Insert(strconv.Itoa(i))
|
|
}
|
|
wg.Done()
|
|
}()
|
|
|
|
wg.Wait()
|
|
fmt.Println(buf.messages)
|
|
fmt.Println(readMsg)
|
|
}
|