Skip to content

Commit cd9a4d1

Browse files
committed
Tests and formatting
1 parent a4b1f71 commit cd9a4d1

6 files changed

Lines changed: 142 additions & 123 deletions

File tree

internal/app/queue/egress_queue_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ func TestEgressQueue_Enqueue(t *testing.T) {
6161
payload := []byte("test-payload")
6262

6363
tests := []struct {
64-
name string
65-
setup func(*queue.EgressQueue)
66-
ctx func() context.Context
67-
payload []byte
68-
expect_err error
64+
name string
65+
setup func(*queue.EgressQueue)
66+
ctx func() context.Context
67+
payload []byte
68+
expect_err error
6969
}{
7070
{
7171
name: "enqueues payload successfully",

internal/app/router/router_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ func TestHandle(t *testing.T) {
2626
const id = "sub-1"
2727

2828
tests := []struct {
29-
name string
30-
msg any
31-
setup func(*pending.Registry)
32-
receive *mock_receive_data
33-
expect_err bool
34-
verify func(*testing.T, *pending.Registry, *mock_receive_data)
29+
name string
30+
msg any
31+
setup func(*pending.Registry)
32+
receive *mock_receive_data
33+
expect_err bool
34+
verify func(*testing.T, *pending.Registry, *mock_receive_data)
3535
}{
3636
{
3737
name: "error message without id returns error",

internal/app/services/connection/create_connection_test.go

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"net/url"
7+
"slices"
78
"testing"
89
"time"
910

@@ -49,9 +50,9 @@ func (m *mock_connection_authorizer) Authorize(_ context.Context, conn connectio
4950

5051
type mock_connection struct{}
5152

52-
func (m *mock_connection) Read(_ context.Context) ([]byte, error) { return nil, nil }
53+
func (m *mock_connection) Read(_ context.Context) ([]byte, error) { return nil, nil }
5354
func (m *mock_connection) Write(_ context.Context, _ []byte) error { return nil }
54-
func (m *mock_connection) Close(_ context.Context) error { return nil }
55+
func (m *mock_connection) Close(_ context.Context) error { return nil }
5556

5657
func TestConnect(t *testing.T) {
5758
gen_err := errors.New("generate failed")
@@ -70,36 +71,60 @@ func TestConnect(t *testing.T) {
7071
expect_err error
7172
expect_dialed bool
7273
expect_authorized bool
74+
verify func(*testing.T, *mock_dialer, *connection.CreateConnectionOutput)
7375
}{
7476
{
7577
name: "success",
7678
generator: &mock_subprotocol_generator{result: "header-xyz"},
7779
dialer: &mock_dialer{conn: conn},
7880
authorizer: &mock_connection_authorizer{timeout: timeout},
7981
input: connection.CreateConnectionInput{Url: endpoint},
80-
expect_err: nil,
8182
expect_dialed: true,
8283
expect_authorized: true,
84+
verify: func(t *testing.T, dialer *mock_dialer, output *connection.CreateConnectionOutput) {
85+
if dialer.received.Url != endpoint {
86+
t.Errorf("dialer.received.Url = %v, want %v", dialer.received.Url, endpoint)
87+
}
88+
got := dialer.received.Subprotocols
89+
if len(got) == 0 || got[len(got)-1] != "header-xyz" {
90+
t.Errorf("dialer subprotocols last element = %v, want %q", got, "header-xyz")
91+
}
92+
if output == nil {
93+
t.Fatal("expected non-nil output")
94+
}
95+
if output.Connection != conn {
96+
t.Errorf("output.Connection = %v, want %v", output.Connection, conn)
97+
}
98+
if output.Timeout != timeout {
99+
t.Errorf("output.Timeout = %v, want %v", output.Timeout, timeout)
100+
}
101+
},
83102
},
84103
{
85-
name: "subprotocol error does not call dialer",
86-
generator: &mock_subprotocol_generator{err: gen_err},
87-
dialer: &mock_dialer{},
88-
authorizer: &mock_connection_authorizer{},
89-
input: connection.CreateConnectionInput{Url: endpoint},
90-
expect_err: gen_err,
91-
expect_dialed: false,
92-
expect_authorized: false,
104+
name: "subprotocol error does not call dialer",
105+
generator: &mock_subprotocol_generator{err: gen_err},
106+
dialer: &mock_dialer{},
107+
authorizer: &mock_connection_authorizer{},
108+
input: connection.CreateConnectionInput{Url: endpoint},
109+
expect_err: gen_err,
93110
},
94111
{
95-
name: "dialer error does not call authorizer",
96-
generator: &mock_subprotocol_generator{result: "header-xyz"},
97-
dialer: &mock_dialer{err: dial_err},
98-
authorizer: &mock_connection_authorizer{},
99-
input: connection.CreateConnectionInput{Url: endpoint},
100-
expect_err: dial_err,
101-
expect_dialed: true,
102-
expect_authorized: false,
112+
name: "dialer error does not call authorizer",
113+
generator: &mock_subprotocol_generator{result: "header-xyz"},
114+
dialer: &mock_dialer{err: dial_err},
115+
authorizer: &mock_connection_authorizer{},
116+
input: connection.CreateConnectionInput{Url: endpoint},
117+
expect_err: dial_err,
118+
expect_dialed: true,
119+
verify: func(t *testing.T, dialer *mock_dialer, _ *connection.CreateConnectionOutput) {
120+
if dialer.received.Url != endpoint {
121+
t.Errorf("dialer.received.Url = %v, want %v", dialer.received.Url, endpoint)
122+
}
123+
got := dialer.received.Subprotocols
124+
if len(got) == 0 || got[len(got)-1] != "header-xyz" {
125+
t.Errorf("dialer subprotocols last element = %v, want %q", got, "header-xyz")
126+
}
127+
},
103128
},
104129
{
105130
name: "authorizer error is returned",
@@ -110,6 +135,15 @@ func TestConnect(t *testing.T) {
110135
expect_err: auth_err,
111136
expect_dialed: true,
112137
expect_authorized: true,
138+
verify: func(t *testing.T, dialer *mock_dialer, _ *connection.CreateConnectionOutput) {
139+
if dialer.received.Url != endpoint {
140+
t.Errorf("dialer.received.Url = %v, want %v", dialer.received.Url, endpoint)
141+
}
142+
got := dialer.received.Subprotocols
143+
if len(got) == 0 || got[len(got)-1] != "header-xyz" {
144+
t.Errorf("dialer subprotocols last element = %v, want %q", got, "header-xyz")
145+
}
146+
},
113147
},
114148
{
115149
name: "generated subprotocol appended to input subprotocols",
@@ -120,19 +154,34 @@ func TestConnect(t *testing.T) {
120154
Url: endpoint,
121155
Subprotocols: []string{"header-abc"},
122156
},
123-
expect_err: nil,
124157
expect_dialed: true,
125158
expect_authorized: true,
159+
verify: func(t *testing.T, dialer *mock_dialer, output *connection.CreateConnectionOutput) {
160+
got := dialer.received.Subprotocols
161+
if len(got) == 0 || got[len(got)-1] != "header-xyz" {
162+
t.Errorf("dialer subprotocols last element = %v, want %q", got, "header-xyz")
163+
}
164+
if !slices.Contains(got, "header-abc") {
165+
t.Errorf("input subprotocol %q missing from dialer received %v", "header-abc", got)
166+
}
167+
if output == nil {
168+
t.Fatal("expected non-nil output")
169+
}
170+
},
126171
},
127172
{
128173
name: "input URL forwarded to dialer",
129174
generator: &mock_subprotocol_generator{result: "header-xyz"},
130175
dialer: &mock_dialer{conn: conn},
131176
authorizer: &mock_connection_authorizer{timeout: timeout},
132177
input: connection.CreateConnectionInput{Url: endpoint},
133-
expect_err: nil,
134178
expect_dialed: true,
135179
expect_authorized: true,
180+
verify: func(t *testing.T, dialer *mock_dialer, _ *connection.CreateConnectionOutput) {
181+
if dialer.received.Url != endpoint {
182+
t.Errorf("dialer.received.Url = %v, want %v", dialer.received.Url, endpoint)
183+
}
184+
},
136185
},
137186
}
138187

@@ -145,50 +194,14 @@ func TestConnect(t *testing.T) {
145194
if !errors.Is(err, tt.expect_err) {
146195
t.Errorf("got error %v, want %v", err, tt.expect_err)
147196
}
148-
149197
if tt.dialer.called != tt.expect_dialed {
150198
t.Errorf("dialer.called = %v, want %v", tt.dialer.called, tt.expect_dialed)
151199
}
152-
153200
if tt.authorizer.called != tt.expect_authorized {
154201
t.Errorf("authorizer.called = %v, want %v", tt.authorizer.called, tt.expect_authorized)
155202
}
156-
157-
if tt.expect_dialed {
158-
if tt.dialer.received.Url != tt.input.Url {
159-
t.Errorf("dialer.received.Url = %v, want %v", tt.dialer.received.Url, tt.input.Url)
160-
}
161-
162-
expected_last := tt.generator.result
163-
got := tt.dialer.received.Subprotocols
164-
if len(got) == 0 || got[len(got)-1] != expected_last {
165-
t.Errorf("dialer subprotocols last element = %v, want %q", got, expected_last)
166-
}
167-
168-
for _, p := range tt.input.Subprotocols {
169-
found := false
170-
for _, g := range got {
171-
if g == p {
172-
found = true
173-
break
174-
}
175-
}
176-
if !found {
177-
t.Errorf("input subprotocol %q missing from dialer received %v", p, got)
178-
}
179-
}
180-
}
181-
182-
if err == nil {
183-
if output == nil {
184-
t.Fatal("expected non-nil output")
185-
}
186-
if output.Connection != conn {
187-
t.Errorf("output.Connection = %v, want %v", output.Connection, conn)
188-
}
189-
if output.Timeout != tt.authorizer.timeout {
190-
t.Errorf("output.Timeout = %v, want %v", output.Timeout, tt.authorizer.timeout)
191-
}
203+
if tt.verify != nil {
204+
tt.verify(t, tt.dialer, output)
192205
}
193206
})
194207
}

internal/app/services/connection/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import "github.com/exanubes/appsync/internal/app"
44

55
type noop_logger struct{}
66

7-
func (n *noop_logger) Debug(_ string, _ ...any) {}
7+
func (n *noop_logger) Debug(_ string, _ ...any) {}
88
func (n *noop_logger) SetContext(_ string) app.Logger { return n }

internal/app/services/subscription/unsubscribe_test.go

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ func TestUnsubscribe(t *testing.T) {
8484
signature := app.Signature{"Authorization": "sig-value"}
8585

8686
tests := []struct {
87-
name string
88-
registry *mock_unsubscribe_registry
89-
authorizer *mock_authorizer
90-
sender *mock_sender
91-
expect_err error
92-
expect_send bool
93-
expect_sub_closed bool
94-
expect_removed string
87+
name string
88+
registry *mock_unsubscribe_registry
89+
authorizer *mock_authorizer
90+
sender *mock_sender
91+
expect_err error
92+
expect_send bool
93+
verify func(*testing.T, *mock_frame_builder, *mock_unsubscribe_registry)
9594
}{
9695
{
9796
name: "subscription not found",
@@ -121,16 +120,51 @@ func TestUnsubscribe(t *testing.T) {
121120
sender: &mock_sender{err: send_err},
122121
expect_err: send_err,
123122
expect_send: true,
123+
verify: func(t *testing.T, frame *mock_frame_builder, registry *mock_unsubscribe_registry) {
124+
if frame.frame_type != protocol.TypeUnsubscribe {
125+
t.Errorf("frame.frame_type = %q, want %q", frame.frame_type, protocol.TypeUnsubscribe)
126+
}
127+
if frame.id != subscriptionId {
128+
t.Errorf("frame.id = %q, want %q", frame.id, subscriptionId)
129+
}
130+
for k, v := range signature {
131+
if frame.signature[k] != v {
132+
t.Errorf("frame.signature[%q] = %q, want %q", k, frame.signature[k], v)
133+
}
134+
}
135+
if !registry.sub.Active() {
136+
t.Error("subscription should remain active when send fails")
137+
}
138+
if registry.removed != "" {
139+
t.Errorf("registry.removed = %q, want empty", registry.removed)
140+
}
141+
},
124142
},
125143
{
126-
name: "success",
127-
registry: &mock_unsubscribe_registry{sub: active_subscription()},
128-
authorizer: &mock_authorizer{signature: signature},
129-
sender: &mock_sender{},
130-
expect_err: nil,
131-
expect_send: true,
132-
expect_sub_closed: true,
133-
expect_removed: subscriptionId,
144+
name: "success",
145+
registry: &mock_unsubscribe_registry{sub: active_subscription()},
146+
authorizer: &mock_authorizer{signature: signature},
147+
sender: &mock_sender{},
148+
expect_send: true,
149+
verify: func(t *testing.T, frame *mock_frame_builder, registry *mock_unsubscribe_registry) {
150+
if frame.frame_type != protocol.TypeUnsubscribe {
151+
t.Errorf("frame.frame_type = %q, want %q", frame.frame_type, protocol.TypeUnsubscribe)
152+
}
153+
if frame.id != subscriptionId {
154+
t.Errorf("frame.id = %q, want %q", frame.id, subscriptionId)
155+
}
156+
for k, v := range signature {
157+
if frame.signature[k] != v {
158+
t.Errorf("frame.signature[%q] = %q, want %q", k, frame.signature[k], v)
159+
}
160+
}
161+
if registry.sub.Active() {
162+
t.Error("subscription should be closed after successful unsubscribe")
163+
}
164+
if registry.removed != subscriptionId {
165+
t.Errorf("registry.removed = %q, want %q", registry.removed, subscriptionId)
166+
}
167+
},
134168
},
135169
}
136170

@@ -145,39 +179,11 @@ func TestUnsubscribe(t *testing.T) {
145179
if !errors.Is(err, tt.expect_err) {
146180
t.Errorf("got error %v, want %v", err, tt.expect_err)
147181
}
148-
149182
if tt.sender.called != tt.expect_send {
150183
t.Errorf("sender.called = %v, want %v", tt.sender.called, tt.expect_send)
151184
}
152-
153-
if tt.expect_send {
154-
if frame.frame_type != protocol.TypeUnsubscribe {
155-
t.Errorf("frame.frame_type = %q, want %q", frame.frame_type, protocol.TypeUnsubscribe)
156-
}
157-
if frame.id != subscriptionId {
158-
t.Errorf("frame.id = %q, want %q", frame.id, subscriptionId)
159-
}
160-
for k, v := range tt.authorizer.signature {
161-
if frame.signature[k] != v {
162-
t.Errorf("frame.signature[%q] = %q, want %q", k, frame.signature[k], v)
163-
}
164-
}
165-
}
166-
167-
if tt.expect_sub_closed && tt.registry.sub != nil {
168-
if tt.registry.sub.Active() {
169-
t.Error("subscription should be closed after successful unsubscribe")
170-
}
171-
}
172-
173-
if !tt.expect_sub_closed && tt.registry.sub != nil && tt.expect_err != app.ErrSubscriptionClosed {
174-
if !tt.registry.sub.Active() {
175-
t.Error("subscription should remain active when execution fails")
176-
}
177-
}
178-
179-
if tt.registry.removed != tt.expect_removed {
180-
t.Errorf("registry.removed = %q, want %q", tt.registry.removed, tt.expect_removed)
185+
if tt.verify != nil {
186+
tt.verify(t, frame, tt.registry)
181187
}
182188
})
183189
}

internal/app/usecases/shutdown/shutdown_connection_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func TestShutdownConnection(t *testing.T) {
4242
sentinel_trans := errors.New("transport close failed")
4343

4444
tests := []struct {
45-
name string
46-
registry_ids []string
47-
remover_err error
48-
runtime_err error
49-
transport_err error
50-
expect_errors []error
45+
name string
46+
registry_ids []string
47+
remover_err error
48+
runtime_err error
49+
transport_err error
50+
expect_errors []error
5151
}{
5252
{
5353
name: "success",

0 commit comments

Comments
 (0)