From 3b4f23a374f8f46dcefab242e999646530429a13 Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Thu, 1 Feb 2024 13:25:34 +0800 Subject: [PATCH] feat: add initial daemon test, adjust code --- server/daemon/daemon.go | 3 +- server/daemon/daemon_test.go | 49 +++++++++++++++++++++++++++++ server/daemon/server/clients.go | 8 +++++ server/daemon/server/server.go | 31 +++++++++++------- server/daemon/server/server_test.go | 9 ------ 5 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 server/daemon/daemon_test.go delete mode 100644 server/daemon/server/server_test.go diff --git a/server/daemon/daemon.go b/server/daemon/daemon.go index 886c878..7d76a92 100644 --- a/server/daemon/daemon.go +++ b/server/daemon/daemon.go @@ -32,7 +32,8 @@ func Connect(addr string, clientType types.ClientType, handlerFunc ...rpc.Handle } func Serve(addr string) error { - return server.Start(addr) + srv := server.NewServer() + return server.Start(srv, addr) } func Execute(clientType types.ClientType, execFn func(client *Client) error) error { diff --git a/server/daemon/daemon_test.go b/server/daemon/daemon_test.go new file mode 100644 index 0000000..77460b0 --- /dev/null +++ b/server/daemon/daemon_test.go @@ -0,0 +1,49 @@ +package daemon_test + +import ( + "context" + "testing" + + "github.com/nedpals/bugbuddy/server/daemon/client" + "github.com/nedpals/bugbuddy/server/daemon/server" + "github.com/nedpals/bugbuddy/server/daemon/types" +) + +const defaultAddr = ":3434" + +func StartServer() *server.Server { + server := server.NewServer() + go func() { + server.Start(defaultAddr) + }() + return server +} + +func TestHandshake(t *testing.T) { + clientId := 1 + srv := StartServer() + + client := client.NewClient(context.TODO(), defaultAddr, types.MonitorClientType) + client.SetId(clientId) + + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + // check if client is connected + if !client.IsConnected() { + t.Fatalf("expected client to be connected") + } + + // check if the server has a client + gotClientId, gotClientType := server.GetClientInfo(srv, clientId) + if gotClientId != clientId { + t.Fatalf("expected client id %d, got %d", clientId, gotClientId) + } + + if gotClientType != types.MonitorClientType { + t.Fatalf("expected client type %v, got %v", types.MonitorClientType, gotClientType) + } + + defer client.Close() +} diff --git a/server/daemon/server/clients.go b/server/daemon/server/clients.go index d67afd0..f1267f3 100644 --- a/server/daemon/server/clients.go +++ b/server/daemon/server/clients.go @@ -64,3 +64,11 @@ func (clients connectedClients) Disconnect() { delete(clients, cl.id) } } + +func GetClientInfo(s *Server, procId int) (int, types.ClientType) { + c, ok := s.connectedClients[procId] + if ok { + return c.id, c.clientType + } + return -1, types.UnknownClientType +} diff --git a/server/daemon/server/server.go b/server/daemon/server/server.go index 99095e5..d5be01f 100644 --- a/server/daemon/server/server.go +++ b/server/daemon/server/server.go @@ -124,7 +124,7 @@ func (d *Server) Handle(ctx context.Context, c *jsonrpc2.Conn, r *jsonrpc2.Reque }) } - rec, p, err := d.Collect(ctx, payload, c) + rec, p, err := d.collect(ctx, payload, c) if err != nil { fmt.Printf("> collect error: %s\n", err.Error()) c.Reply(ctx, r.ID, map[string]any{ @@ -210,7 +210,7 @@ func (d *Server) Handle(ctx context.Context, c *jsonrpc2.Conn, r *jsonrpc2.Reque } } -func (s *Server) Collect(ctx context.Context, payload types.CollectPayload, c *jsonrpc2.Conn) (recognized int, processed int, err error) { +func (s *Server) collect(ctx context.Context, payload types.CollectPayload, c *jsonrpc2.Conn) (recognized int, processed int, err error) { result := helpers.AnalyzeError(s.engine, payload.WorkingDir, payload.Error) if result.Err != nil { return result.Stats() @@ -259,12 +259,15 @@ func (s *Server) notifyErrors(ctx context.Context, procIds_ ...int) { } } -func Start(addr string) error { - isTerminal := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) - errChan := make(chan error, 1) - disconnChan := make(chan int, 1) - exitSignal := make(chan os.Signal, 1) +func (s *Server) Start(addr string) error { + return rpc.StartServer( + addr, + jsonrpc2.VarintObjectCodec{}, + s, + ) +} +func NewServer() *Server { server := &Server{ engine: &errgoengine.ErrgoEngine{ ErrorTemplates: errgoengine.ErrorTemplates{}, @@ -282,14 +285,18 @@ func Start(addr string) error { } error_templates.LoadErrorTemplates(&server.engine.ErrorTemplates) + return server +} + +func Start(server *Server, addr string) error { + isTerminal := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) + errChan := make(chan error, 1) + disconnChan := make(chan int, 1) + exitSignal := make(chan os.Signal, 1) go func() { fmt.Println("> daemon started on " + addr) - errChan <- rpc.StartServer( - addr, - jsonrpc2.VarintObjectCodec{}, - server, - ) + errChan <- server.Start(addr) }() signal.Notify(exitSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) diff --git a/server/daemon/server/server_test.go b/server/daemon/server/server_test.go deleted file mode 100644 index 7127342..0000000 --- a/server/daemon/server/server_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package server - -import "testing" - -// TODO: test - -func TestConnection(t *testing.T) { - // TODO: -}