Files
genai-toolbox/internal/server/server_test.go
Kurtis Van Gent efe9e8bc97 chore: refactor sources/tools each into their own package (#42)
Moves all of the "source" and "tool" implementations into their own
packages. This layout makes it a bit more clear where the
implementations are, and seems likely to scale more cleanly as more
sources and tools are added.
2024-11-01 16:44:13 -06:00

70 lines
1.6 KiB
Go

// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server_test
import (
"context"
"net"
"strconv"
"testing"
"time"
"github.com/googleapis/genai-toolbox/internal/server"
)
// tryDial is a utility function that dials an address up to 'attempts' number of times.
func tryDial(addr string, attempts int) bool {
for i := 0; i < attempts; i++ {
conn, err := net.Dial("tcp", addr)
if err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
_ = conn.Close()
return true
}
return false
}
func TestServe(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, port := "127.0.0.1", 5000
cfg := server.ServerConfig{
Address: addr,
Port: port,
}
s, err := server.NewServer(cfg)
if err != nil {
t.Fatalf("Unable to initialize server! %v", err)
}
// start server in background
errCh := make(chan error)
go func() {
err := s.ListenAndServe(ctx)
defer close(errCh)
if err != nil {
errCh <- err
}
}()
if !tryDial(net.JoinHostPort(addr, strconv.Itoa(port)), 10) {
t.Fatalf("Unable to dial server!")
}
}