Skip to content

Commit

Permalink
feat: add initial daemon test, adjust code
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 1, 2024
1 parent 727f2f2 commit 3b4f23a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
3 changes: 2 additions & 1 deletion server/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
49 changes: 49 additions & 0 deletions server/daemon/daemon_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
8 changes: 8 additions & 0 deletions server/daemon/server/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 19 additions & 12 deletions server/daemon/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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{},
Expand All @@ -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)
Expand Down
9 changes: 0 additions & 9 deletions server/daemon/server/server_test.go

This file was deleted.

0 comments on commit 3b4f23a

Please sign in to comment.