notify/peer_attach_test_helper_test.go

56 lines
1.4 KiB
Go
Raw Normal View History

package notify
import (
"b612.me/stario"
"context"
"fmt"
"math"
"net"
"sync/atomic"
"testing"
)
var testAcceptedPeerSeq atomic.Uint64
func newRunningPeerAttachServerForTest(t *testing.T, configure func(*ServerCommon)) *ServerCommon {
t.Helper()
server := NewServer().(*ServerCommon)
if configure != nil {
configure(server)
}
stopCtx, stopFn := context.WithCancel(context.Background())
queue := stario.NewQueueCtx(stopCtx, 8, math.MaxUint32)
server.setServerSessionRuntime(&serverSessionRuntime{
stopCtx: stopCtx,
stopFn: stopFn,
queue: queue,
})
server.markSessionStarted()
transportCtx, transportStop := context.WithCancel(context.Background())
go server.loadMessageLoop(nil, transportCtx, queue, nil, nil)
t.Cleanup(func() {
transportStop()
stopFn()
})
return server
}
func bootstrapPeerAttachLogicalForTest(t *testing.T, server *ServerCommon, conn net.Conn) *LogicalConn {
t.Helper()
if server == nil {
t.Fatal("server is nil")
}
id := fmt.Sprintf("accepted-%d", testAcceptedPeerSeq.Add(1))
logical := server.bootstrapAcceptedLogical(id, conn.RemoteAddr(), conn)
if logical == nil {
t.Fatal("bootstrapAcceptedLogical returned nil")
}
return logical
}
func bootstrapPeerAttachConnForTest(t *testing.T, server *ServerCommon, conn net.Conn) *ClientConn {
return clientConnFromLogical(bootstrapPeerAttachLogicalForTest(t, server, conn))
}