2023-11-30 01:57:45 +00:00
|
|
|
package service_test
|
|
|
|
|
|
|
|
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"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/CPunch/gopenfusion/internal/protocol"
|
|
|
|
"github.com/CPunch/gopenfusion/internal/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
srvcPort int
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-12-01 06:56:34 +00:00
|
|
|
timeout = 2
|
2023-11-30 01:57:45 +00:00
|
|
|
maxDummyPeers = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
func waitWithTimeout(wg *sync.WaitGroup, seconds int) bool {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return true
|
|
|
|
case <-time.After(time.Duration(seconds) * time.Second):
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
|
|
|
srvcPort, err = service.RandomPort()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
2023-12-01 06:56:34 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
srvc := service.NewService(ctx, "TEST", srvcPort)
|
|
|
|
|
2023-11-30 01:57:45 +00:00
|
|
|
// waitgroup to wait for test packet handler to be called
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
2023-12-01 06:56:34 +00:00
|
|
|
srvc.AddPacketHandler(0x1234, func(peer *protocol.CNPeer, pkt protocol.Packet) error {
|
|
|
|
log.Printf("Received packet %#v", pkt)
|
2023-11-30 01:57:45 +00:00
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := srvc.Start(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for service to start
|
|
|
|
<-srvc.Started()
|
2023-12-01 06:56:34 +00:00
|
|
|
wg.Add(maxDummyPeers * 3) // 2 wg.Done() calls per dummy peer
|
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))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:34 +00:00
|
|
|
peer := protocol.NewCNPeer(ctx, conn)
|
|
|
|
go func() {
|
|
|
|
defer peer.Kill()
|
|
|
|
|
|
|
|
// send dummy packets
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if err := peer.Send(0x1234); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// we wait until Handler gracefully exits (peer was killed)
|
|
|
|
peer.Handler(make(chan *protocol.PacketEvent))
|
|
|
|
wg.Done()
|
2023-11-30 01:57:45 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !waitWithTimeout(&wg, timeout) {
|
2023-12-01 06:56:34 +00:00
|
|
|
t.Error("failed to wait for packet handler to be called")
|
2023-11-30 01:57:45 +00:00
|
|
|
}
|
2023-12-01 06:56:34 +00:00
|
|
|
|
|
|
|
cancel()
|
2023-11-30 01:57:45 +00:00
|
|
|
<-srvc.Stopped()
|
|
|
|
}
|