chore: added basic server startup test (#817)

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
Alex Jones 2023-12-29 14:15:32 +00:00 committed by GitHub
parent fcd29a547d
commit 3e7cea7bd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

View File

@ -42,6 +42,7 @@ type Config struct {
Handler *handler
Logger *zap.Logger
metricsServer *http.Server
listener net.Listener
}
type Health struct {
@ -56,6 +57,10 @@ var health = Health{
Failure: 0,
}
func (s *Config) Shutdown() error {
return s.listener.Close()
}
func (s *Config) Serve() error {
var lis net.Listener
@ -65,6 +70,7 @@ func (s *Config) Serve() error {
if err != nil {
return err
}
s.listener = lis
s.Logger.Info(fmt.Sprintf("binding api to %s", s.Port))
grpcServerUnaryInterceptor := grpc.UnaryInterceptor(logInterceptor(s.Logger))
grpcServer := grpc.NewServer(grpcServerUnaryInterceptor)

41
pkg/server/server_test.go Normal file
View File

@ -0,0 +1,41 @@
package server
import (
"github.com/fatih/color"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"os"
"sync"
"testing"
)
func TestServerInit(t *testing.T) {
logger, err := zap.NewDevelopment()
if err != nil {
color.Red("failed to create logger: %v", err)
os.Exit(1)
}
defer logger.Sync()
server_config := Config{
Backend: "openai",
Port: "0",
MetricsPort: "0",
Token: "none",
Logger: logger,
}
var wg sync.WaitGroup
go func() {
wg.Add(1)
err := server_config.Serve()
if err != nil {
assert.Fail(t, "serve: %s", err.Error())
}
server_config.Shutdown()
if err != nil {
assert.Fail(t, "shutdown: %s", err.Error())
}
wg.Done()
}()
wg.Wait()
}