mchess-server/connection/message_buffer_test.go
Marco ae3e73f711 Another step towards allowing reconnections
1. Implement thread-safe ringbuffer for websocket messages

This implements a ringbuffer that is used to decouple the raw websocket
connection from the messages that the game handler handles.

2. Change websocket handling

With this commit, we stop waiting for the websocket connection to be
established before the game starts.
Now, the Connection type is responsible for waiting for the websocket
connection before writing.

Some bugs are still happening:
	1. The rejoining client is not told the state of the board
	2. Invalid moves are not handled by the client (not sure why though)
	3. The still-connected client should be told, that the opponent
	   disconnected. Then the client should show the passphrase again

3. Introduce method to send status of board and player

4. Reconnect works (kind of)

With the right changes in the client, the reconnect works (but only for
the first time).

WARNING:
At the moment, we will create a new player whenever connection wants to
join a private game. This will also clear all the disconnect callbacks
that we set in the player.
2023-12-09 21:00:52 +01:00

200 lines
3.7 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, err := buf.Get()
assert.NoError(t, err)
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, err := buf.Get()
assert.NoError(t, err)
assert.Equal(t, "message-1", msg)
go func() {
timer := time.NewTimer(100 * time.Millisecond)
<-timer.C
buf.Insert("delayed-message")
}()
msg, err = buf.Get()
assert.NoError(t, err)
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, err := buf.Get()
assert.NoError(t, err)
assert.Equal(t, "message-2", msg)
}
func Test_MessageBuffer_GetWaitsForNewDataIfOldOneWasAlreadyGotten(t *testing.T) {
buf := newMessageBuffer(2)
buf.Insert(message1)
msg, err := buf.Get()
assert.NoError(t, err)
assert.Equal(t, message1, msg)
buf.Insert(message2)
msg, err = buf.Get()
assert.NoError(t, err)
assert.Equal(t, message2, msg)
go func() {
<-time.NewTimer(100 * time.Millisecond).C
buf.Insert(message3)
}()
msg, err = buf.Get()
assert.NoError(t, err)
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, err := buf.Get()
assert.NoError(t, err)
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)
}