Skip to content

Commit

Permalink
Clean up test code and increase parallelization (#698)
Browse files Browse the repository at this point in the history
Nothing fancy, just cleaning up some tests and adding `t.Parallel()`.
  • Loading branch information
adambabik authored Nov 19, 2024
1 parent ec2e349 commit 09bcad1
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 113 deletions.
4 changes: 2 additions & 2 deletions internal/project/projectservice/project_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestProjectServiceServer_Load(t *testing.T) {
lis, stop := testStartProjectServiceServer(t)
t.Cleanup(stop)

_, client := testutils.NewTestGRPCClient(t, lis, projectv1.NewProjectServiceClient)
_, client := testutils.NewGRPCClientWithT(t, lis, projectv1.NewProjectServiceClient)

t.Run("GitProject", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestProjectServiceServer_Load_ClientConnClosed(t *testing.T) {
lis, stop := testStartProjectServiceServer(t)
t.Cleanup(stop)

clientConn, client := testutils.NewTestGRPCClient(t, lis, projectv1.NewProjectServiceClient)
clientConn, client := testutils.NewGRPCClientWithT(t, lis, projectv1.NewProjectServiceClient)

req := &projectv1.LoadRequest{
Kind: &projectv1.LoadRequest_File{
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func Test_runnerService(t *testing.T) {
lis, stop := testStartRunnerServiceServer(t)
t.Cleanup(stop)

_, client := testutils.NewTestGRPCClient(t, lis, runnerv1.NewRunnerServiceClient)
_, client := testutils.NewGRPCClientWithT(t, lis, runnerv1.NewRunnerServiceClient)

t.Run("Sessions", func(t *testing.T) {
t.Parallel()
Expand Down
31 changes: 8 additions & 23 deletions internal/runnerv2client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"google.golang.org/grpc/test/bufconn"

"github.com/stateful/runme/v3/internal/command"
"github.com/stateful/runme/v3/internal/runnerv2service"
"github.com/stateful/runme/v3/internal/testutils/runnerservice"
runnerv2 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v2"
)

Expand All @@ -25,13 +25,14 @@ func init() {
func TestClient_ExecuteProgram(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
lis, stop := runnerservice.New(t)
t.Cleanup(stop)

t.Run("OutputWithSession", func(t *testing.T) {
t.Parallel()

client := testCreateClient(t, lis)
client := createClient(t, lis)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

Expand Down Expand Up @@ -74,7 +75,8 @@ func TestClient_ExecuteProgram(t *testing.T) {
t.Run("InputNonInteractive", func(t *testing.T) {
t.Parallel()

client := testCreateClient(t, lis)
client := createClient(t, lis)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

Expand Down Expand Up @@ -104,29 +106,12 @@ func TestClient_ExecuteProgram(t *testing.T) {
})
}

// TODO(adamb): it's copied from internal/runnerv2service.
func testStartRunnerServiceServer(t *testing.T) (*bufconn.Listener, func()) {
func createClient(t *testing.T, lis *bufconn.Listener) *Client {
t.Helper()

logger := zaptest.NewLogger(t)
factory := command.NewFactory(command.WithLogger(logger))

server := grpc.NewServer()

runnerService, err := runnerv2service.NewRunnerService(factory, logger)
require.NoError(t, err)
runnerv2.RegisterRunnerServiceServer(server, runnerService)

lis := bufconn.Listen(1024 << 10)
go server.Serve(lis)

return lis, server.Stop
}

func testCreateClient(t *testing.T, lis *bufconn.Listener) *Client {
client, err := New(
"passthrough://bufconn",
zaptest.NewLogger(t),
logger,
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}),
Expand Down
6 changes: 4 additions & 2 deletions internal/runnerv2client/client_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import (
"time"

"github.com/stateful/runme/v3/internal/command"
"github.com/stateful/runme/v3/internal/testutils/runnerservice"
runnerv2 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v2"
"github.com/stretchr/testify/require"
)

func TestClient_ExecuteProgram_InputInteractive(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
lis, stop := runnerservice.New(t)
t.Cleanup(stop)

client := testCreateClient(t, lis)
client := createClient(t, lis)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

Expand Down
Loading

0 comments on commit 09bcad1

Please sign in to comment.