Skip to content

Commit

Permalink
feat: testupdate (#1315)
Browse files Browse the repository at this point in the history
* feat: updated server test function

Signed-off-by: AlexsJones <[email protected]>

* chore: fixed server test

Signed-off-by: AlexsJones <[email protected]>

* chore: updated go.sum

Signed-off-by: AlexsJones <[email protected]>

* feat: added free disk space to release job

Signed-off-by: AlexsJones <[email protected]>

---------

Signed-off-by: AlexsJones <[email protected]>
  • Loading branch information
AlexsJones authored Nov 10, 2024
1 parent 44f4f74 commit 7dcdfc8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/websocket v1.5.2 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,8 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.2 h1:qoW6V1GT3aZxybsbC6oLnailWnB+qTMVwMreOso9XUw=
github.com/gorilla/websocket v1.5.2/go.mod h1:0n9H61RBAcf5/38py2MCYbxzPIY9rOkpvvMT24Rqs30=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
Expand Down
71 changes: 49 additions & 22 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
package server

import (
"os"
"context"
"net"
"testing"
"time"

"github.com/fatih/color"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)

func TestServerInit(t *testing.T) {
logger, err := zap.NewDevelopment()
if err != nil {
color.Red("failed to create logger: %v", err)
os.Exit(1)
}
//nolint:all
func TestServe(t *testing.T) {
logger, _ := zap.NewDevelopment()
defer logger.Sync()
server_config := Config{
Backend: "openai",
Port: "0",
MetricsPort: "0",
Token: "none",
Logger: logger,

s := &Config{
Port: "50059",
Logger: logger,
EnableHttp: false,
}

go func() {
err := server_config.Serve()
if err != nil {
assert.Fail(t, "serve: %s", err.Error())
err := s.Serve()
time.Sleep(time.Second * 2)
assert.NoError(t, err, "Serve should not return an error")
}()

// Wait until the server is ready to accept connections
err := waitForPort("localhost:50059", 10*time.Second)
assert.NoError(t, err, "Server should start without error")

conn, err := grpc.Dial("localhost:50059", grpc.WithInsecure())
assert.NoError(t, err, "Should be able to dial the server")
defer conn.Close()

// Test a simple gRPC reflection request
cli := grpc_reflection_v1alpha.NewServerReflectionClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := cli.ServerReflectionInfo(ctx)
assert.NoError(t, err, "Should be able to get server reflection info")
assert.NotNil(t, resp, "Response should not be nil")

// Cleanup
err = s.Shutdown()
assert.NoError(t, err, "Shutdown should not return an error")
}

func waitForPort(address string, timeout time.Duration) error {
start := time.Now()
for {
conn, err := net.Dial("tcp", address)
if err == nil {
conn.Close()
return nil
}
err = server_config.Shutdown()
if err != nil {
assert.Fail(t, "shutdown: %s", err.Error())
if time.Since(start) > timeout {
return err
}
}()
time.Sleep(100 * time.Millisecond)
}
}

0 comments on commit 7dcdfc8

Please sign in to comment.