test: close servers

This commit is contained in:
Kevin Franklin Kim 2024-11-25 22:23:12 +01:00
parent ea920005e9
commit e50df5eaf9
No known key found for this signature in database
3 changed files with 17 additions and 25 deletions

View File

@ -27,12 +27,12 @@ doc:
.PHONY: test .PHONY: test
## Run tests ## Run tests
test: test:
@go test -coverprofile=coverage.out -race -json ./... | gotestfmt @GO_TEST_TAGS=-skip go test -coverprofile=coverage.out -race -json ./... | gotestfmt
.PHONY: test.update .PHONY: test.update
## Run tests and update snapshots ## Run tests and update snapshots
test.update: test.update:
@go test -update -coverprofile=coverage.out -race -json ./... | gotestfmt @GO_TEST_TAGS=-skip go test -update -coverprofile=coverage.out -race -json ./... | gotestfmt
.PHONY: lint .PHONY: lint
## Run linter ## Run linter
@ -61,7 +61,7 @@ install:
## Build binary ## Build binary
build: build:
@mkdir -p bin @mkdir -p bin
@go build -o bin/sesamy main.go @go build -o bin/contentserver main.go
## === Utils === ## === Utils ===

View File

@ -18,7 +18,7 @@ import (
) )
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
testWithClients(t, func(c *client.Client) { testWithClients(t, func(t *testing.T, c *client.Client) {
response, err := c.Update(context.TODO()) response, err := c.Update(context.TODO())
require.NoError(t, err) require.NoError(t, err)
require.True(t, response.Success, "update has to return .Sucesss true") require.True(t, response.Success, "update has to return .Sucesss true")
@ -28,7 +28,7 @@ func TestUpdate(t *testing.T) {
} }
func TestGetURIs(t *testing.T) { func TestGetURIs(t *testing.T) {
testWithClients(t, func(c *client.Client) { testWithClients(t, func(t *testing.T, c *client.Client) {
request := mock.MakeValidURIsRequest() request := mock.MakeValidURIsRequest()
uriMap, err := c.GetURIs(context.TODO(), request.Dimension, request.IDs) uriMap, err := c.GetURIs(context.TODO(), request.Dimension, request.IDs)
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
@ -38,7 +38,7 @@ func TestGetURIs(t *testing.T) {
} }
func TestGetRepo(t *testing.T) { func TestGetRepo(t *testing.T) {
testWithClients(t, func(c *client.Client) { testWithClients(t, func(t *testing.T, c *client.Client) {
r, err := c.GetRepo(context.TODO()) r, err := c.GetRepo(context.TODO())
require.NoError(t, err) require.NoError(t, err)
if assert.NotEmpty(t, r, "received empty JSON from GetRepo") { if assert.NotEmpty(t, r, "received empty JSON from GetRepo") {
@ -48,7 +48,7 @@ func TestGetRepo(t *testing.T) {
} }
func TestGetNodes(t *testing.T) { func TestGetNodes(t *testing.T) {
testWithClients(t, func(c *client.Client) { testWithClients(t, func(t *testing.T, c *client.Client) {
nodesRequest := mock.MakeNodesRequest() nodesRequest := mock.MakeNodesRequest()
nodes, err := c.GetNodes(context.TODO(), nodesRequest.Env, nodesRequest.Nodes) nodes, err := c.GetNodes(context.TODO(), nodesRequest.Env, nodesRequest.Nodes)
require.NoError(t, err) require.NoError(t, err)
@ -67,20 +67,12 @@ func TestGetNodes(t *testing.T) {
} }
func TestGetContent(t *testing.T) { func TestGetContent(t *testing.T) {
testWithClients(t, func(c *client.Client) { testWithClients(t, func(t *testing.T, c *client.Client) {
request := mock.MakeValidContentRequest() request := mock.MakeValidContentRequest()
response, err := c.GetContent(context.TODO(), request) response, err := c.GetContent(context.TODO(), request)
if err != nil { require.NoError(t, err)
t.Fatal("unexpected err", err) assert.Equal(t, request.URI, response.URI)
} assert.Equal(t, response.Status, content.StatusOk)
if request.URI != response.URI {
dump(t, request)
dump(t, response)
t.Fatal("uri mismatch")
}
if response.Status != content.StatusOk {
t.Fatal("unexpected status")
}
}) })
} }
@ -121,7 +113,7 @@ func benchmarkClientAndServerGetContent(tb testing.TB, numGroups, numCalls int,
wg.Wait() wg.Wait()
} }
func testWithClients(t *testing.T, testFunc func(c *client.Client)) { func testWithClients(t *testing.T, testFunc func(t *testing.T, c *client.Client)) {
t.Helper() t.Helper()
l := zaptest.NewLogger(t) l := zaptest.NewLogger(t)
httpRepoServer := initHTTPRepoServer(t, l) httpRepoServer := initHTTPRepoServer(t, l)
@ -129,12 +121,13 @@ func testWithClients(t *testing.T, testFunc func(c *client.Client)) {
httpClient := newHTTPClient(t, httpRepoServer) httpClient := newHTTPClient(t, httpRepoServer)
socketClient := newSocketClient(t, socketRepoServer.Addr().String()) socketClient := newSocketClient(t, socketRepoServer.Addr().String())
defer func() { defer func() {
httpRepoServer.Close()
socketRepoServer.Close()
httpClient.Close() httpClient.Close()
socketClient.Close() socketClient.Close()
socketRepoServer.Close()
}() }()
testFunc(httpClient) testFunc(t, httpClient)
testFunc(socketClient) testFunc(t, socketClient)
} }
func initRepo(tb testing.TB, l *zap.Logger) *repo.Repo { func initRepo(tb testing.TB, l *zap.Logger) *repo.Repo {

View File

@ -2,7 +2,6 @@ package repo
import ( import (
"context" "context"
"strings"
"testing" "testing"
"time" "time"
@ -124,7 +123,7 @@ func TestLoadRepoDuplicateUris(t *testing.T) {
response := r.Update() response := r.Update()
require.False(t, response.Success, "there are duplicates, this repo update should have failed") require.False(t, response.Success, "there are duplicates, this repo update should have failed")
assert.True(t, strings.Contains(response.ErrorMessage, "update dimension"), "error message not as expected: "+response.ErrorMessage) assert.Contains(t, response.ErrorMessage, "update dimension")
} }
func TestDimensionHygiene(t *testing.T) { func TestDimensionHygiene(t *testing.T) {