2023-12-02 01:22:49 +00:00
|
|
|
package cnet_test
|
2023-11-30 01:57:45 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-01 06:56:34 +00:00
|
|
|
"context"
|
2023-11-30 01:57:45 +00:00
|
|
|
"fmt"
|
2023-12-01 06:56:34 +00:00
|
|
|
"log"
|
2023-11-30 01:57:45 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2023-12-02 02:23:27 +00:00
|
|
|
"time"
|
2023-11-30 01:57:45 +00:00
|
|
|
|
2023-12-01 23:11:41 +00:00
|
|
|
"github.com/CPunch/gopenfusion/cnet"
|
2023-12-02 01:56:23 +00:00
|
|
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
2023-12-02 02:13:42 +00:00
|
|
|
"github.com/CPunch/gopenfusion/util"
|
2023-12-01 19:49:50 +00:00
|
|
|
"github.com/matryer/is"
|
2023-11-30 01:57:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
srvcPort int
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-12-02 02:23:27 +00:00
|
|
|
timeout = 2 * time.Second
|
2023-11-30 01:57:45 +00:00
|
|
|
maxDummyPeers = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
2023-12-02 01:22:49 +00:00
|
|
|
srvcPort, err = cnet.RandomPort()
|
2023-11-30 01:57:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
2023-12-01 19:49:50 +00:00
|
|
|
is := is.New(t)
|
2023-12-01 06:56:34 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-12-02 01:22:49 +00:00
|
|
|
srvc := cnet.NewService(ctx, "TEST", srvcPort)
|
2023-11-30 01:57:45 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
2023-12-01 19:49:50 +00:00
|
|
|
// shutdown service when test is done
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
2023-12-02 02:13:42 +00:00
|
|
|
is.True(util.SelectWithTimeout(srvc.Stopped(), timeout)) // wait for service to stop with timeout
|
2023-12-01 19:49:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// our dummy packet handler
|
2023-12-01 20:08:17 +00:00
|
|
|
wg.Add(maxDummyPeers)
|
2023-12-02 01:15:00 +00:00
|
|
|
srvc.AddPacketHandler(0x1234, func(peer *cnet.Peer, pkt protocol.Packet) error {
|
2023-12-01 06:56:34 +00:00
|
|
|
log.Printf("Received packet %#v", pkt)
|
2023-11-30 01:57:45 +00:00
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-12-01 19:58:57 +00:00
|
|
|
// wait for all dummy peers to connect and disconnect
|
|
|
|
wg.Add(maxDummyPeers)
|
2023-12-02 01:15:00 +00:00
|
|
|
srvc.OnConnect = func(peer *cnet.Peer) {
|
2023-12-01 19:58:57 +00:00
|
|
|
wg.Done()
|
|
|
|
}
|
2023-11-30 01:57:45 +00:00
|
|
|
|
2023-12-01 19:58:57 +00:00
|
|
|
wg.Add(maxDummyPeers)
|
2023-12-02 01:15:00 +00:00
|
|
|
srvc.OnDisconnect = func(peer *cnet.Peer) {
|
2023-12-01 19:58:57 +00:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// run service
|
2023-12-02 02:13:42 +00:00
|
|
|
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
|
|
|
|
is.True(util.SelectWithTimeout(srvc.Started(), timeout)) // wait for service to start with timeout
|
2023-12-01 19:49:50 +00:00
|
|
|
|
2023-12-01 20:08:17 +00:00
|
|
|
wg.Add(maxDummyPeers * 2) // 2 wg.Done() per peer for receiving packets
|
2023-11-30 01:57:45 +00:00
|
|
|
for i := 0; i < maxDummyPeers; i++ {
|
|
|
|
go func() {
|
|
|
|
// make dummy client
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", srvcPort))
|
2023-12-01 19:49:50 +00:00
|
|
|
is.NoErr(err) // net.Dial error
|
2023-11-30 01:57:45 +00:00
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
peer := cnet.NewPeer(ctx, conn)
|
2023-12-01 06:56:34 +00:00
|
|
|
go func() {
|
|
|
|
defer peer.Kill()
|
|
|
|
|
|
|
|
// send dummy packets
|
|
|
|
for i := 0; i < 2; i++ {
|
2023-12-01 19:58:57 +00:00
|
|
|
is.NoErr(peer.Send(0x1234)) // peer.Send error
|
2023-12-01 06:56:34 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// we wait until Handler gracefully exits (peer was killed)
|
2023-12-01 23:11:41 +00:00
|
|
|
peer.Handler(make(chan *cnet.PacketEvent))
|
2023-12-01 06:56:34 +00:00
|
|
|
wg.Done()
|
2023-11-30 01:57:45 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-12-02 02:13:42 +00:00
|
|
|
is.True(util.WaitWithTimeout(&wg, timeout)) // wait for all dummy peers to be done with timeout
|
2023-11-30 01:57:45 +00:00
|
|
|
}
|