-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
44f4f74
commit 7dcdfc8
Showing
3 changed files
with
52 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |