herald.go/server/notifier_test.go
Jeffrey Picard cd7f20a461
Server endpoints goroutine refactor (#69)
* server.xxx endpoints

Additional server endpoints in jsonrpc and also some refactoring

* server.banner

* more endpoints

* use lbry.go stop pattern

* set genesis hash properly

* updates and refactors

* remove shutdowncalled and itmut

* remove OpenIterators

* remove shutdown and done channels from db and use stop group

* currently broken, incorporated stop groups into the session manager

* set the rest of the default args for tests

* add default json rpc http port and cleanup

* tests for new jsonrpc endpoints

* cleanup and add manage goroutine to stopper pattern

* cleanup

* NewDebug

* asdf

* fix
2022-10-25 08:48:13 +03:00

88 lines
1.7 KiB
Go

package server_test
import (
"encoding/hex"
"fmt"
"net"
"testing"
"time"
"github.com/lbryio/herald.go/internal"
"github.com/lbryio/herald.go/server"
"github.com/lbryio/lbry.go/v3/extras/stop"
"github.com/sirupsen/logrus"
)
const defaultBufferSize = 1024
func tcpConnReady(addr string) (net.Conn, error) {
sleepTime := time.Millisecond * 100
for {
if sleepTime > time.Second {
return nil, fmt.Errorf("timeout")
}
conn, err := net.Dial("tcp", addr)
if err != nil {
logrus.Warn(err)
time.Sleep(sleepTime)
sleepTime = sleepTime * 2
continue
}
return conn, nil
}
}
func tcpRead(conn net.Conn) ([]byte, error) {
buf := make([]byte, defaultBufferSize)
n, err := conn.Read(buf)
if err != nil {
return nil, err
}
if n != server.NotifierResponseLength {
return nil, fmt.Errorf("not all bytes read")
}
return buf[:n], nil
}
func TestNotifierServer(t *testing.T) {
args := server.MakeDefaultTestArgs()
// ctx := context.Background()
ctx := stop.NewDebug()
hub := server.MakeHubServer(ctx, args)
go hub.NotifierServer()
go hub.RunNotifier()
addr := fmt.Sprintf(":%s", args.NotifierPort)
logrus.Info(addr)
conn, err := tcpConnReady(addr)
if err != nil {
t.Fatal(err)
}
resCh := make(chan []byte)
go func() {
logrus.Warn("waiting for response")
res, err := tcpRead(conn)
logrus.Warn("got response")
if err != nil {
logrus.Warn(err)
return
}
resCh <- res
}()
// Hacky but needed because if the reader isn't ready
// before the writer sends it won't get the data
time.Sleep(time.Second)
hash, _ := hex.DecodeString("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
logrus.Warn("sending hash")
hub.NotifierChan <- internal.HeightHash{Height: 1, BlockHash: hash}
res := <-resCh
logrus.Info(string(res))
}