Skip to content

Commit

Permalink
Create separate test function
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Nov 16, 2024
1 parent a7c521f commit 569efb2
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 28 deletions.
60 changes: 60 additions & 0 deletions internal/integration/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 82 additions & 18 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,13 @@ func TestSubscription(t *testing.T) {
_ = `# @genqlient
subscription count { count }`

authKey := server.AuthKey

ctx := context.Background()
server := server.RunServer()
defer server.Close()

cases := []struct {
connParams map[string]interface{}
name string
unsubThreshold time.Duration
counterStart int
expected subscriptionResult
}{
{
Expand All @@ -87,18 +83,6 @@ func TestSubscription(t *testing.T) {
serverChannelClosed: true,
},
},
{
name: "server_closed_authorized_user_gets_incremented_counter",
unsubThreshold: 5 * time.Second,
counterStart: 1000,
connParams: map[string]interface{}{
authKey: "authorized-user-token",
},
expected: subscriptionResult{
clientUnsubscribed: false,
serverChannelClosed: true,
},
},
{
name: "client_unsubscribed",
unsubThreshold: 300 * time.Millisecond,
Expand All @@ -111,7 +95,7 @@ func TestSubscription(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
wsClient := newRoundtripWebSocketClient(t, server.URL, tc.connParams)
wsClient := newRoundtripWebSocketClient(t, server.URL, nil)

errChan, err := wsClient.Start(ctx)
require.NoError(t, err)
Expand All @@ -121,7 +105,7 @@ func TestSubscription(t *testing.T) {
defer wsClient.Close()

var (
counter = tc.counterStart
counter = 0
start = time.Now()
result = subscriptionResult{}
)
Expand Down Expand Up @@ -161,6 +145,86 @@ func TestSubscription(t *testing.T) {
}
}

func TestSubscriptionConnectionParams(t *testing.T) {
_ = `# @genqlient
subscription countAuthorized { countAuthorized }`

authKey := server.AuthKey

ctx := context.Background()
server := server.RunServer()
defer server.Close()

cases := []struct {
connParams map[string]interface{}
name string
expectedError string
}{
{
name: "authorized_user_gets_counter",
connParams: map[string]interface{}{
authKey: "authorized-user-token",
},
},
{
name: "unauthorized_user_gets_error",
expectedError: "input: countAuthorized unauthorized\n",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
wsClient := newRoundtripWebSocketClient(t, server.URL, tc.connParams)

errChan, err := wsClient.Start(ctx)
require.NoError(t, err)

dataChan, subscriptionID, err := countAuthorized(ctx, wsClient)
require.NoError(t, err)
defer wsClient.Close()

var (
counter = 0
start = time.Now()
)

for loop := true; loop; {
select {
case resp, more := <-dataChan:
if !more {
loop = false
break
}

if tc.expectedError != "" {
require.Error(t, resp.Errors)
assert.Equal(t, tc.expectedError, resp.Errors.Error())
continue
}

require.NotNil(t, resp.Data)
assert.Equal(t, counter, resp.Data.CountAuthorized)
require.Nil(t, resp.Errors)

if time.Since(start) > 5*time.Second {
err := wsClient.Unsubscribe(subscriptionID)
require.NoError(t, err)
loop = false
}

counter++

case err := <-errChan:
require.NoError(t, err)

case <-time.After(10 * time.Second):
require.NoError(t, fmt.Errorf("subscription timed out"))
}
}
})
}
}

func TestServerError(t *testing.T) {
_ = `# @genqlient
query failingQuery { fail me { id } }`
Expand Down
1 change: 1 addition & 0 deletions internal/integration/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Mutation {

type Subscription {
count: Int!
countAuthorized: Int!
}

type User implements Being & Lucky {
Expand Down
71 changes: 70 additions & 1 deletion internal/integration/server/gqlgen_exec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions internal/integration/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,37 @@ func getAuthToken(ctx context.Context) string {
}

func (s *subscriptionResolver) Count(ctx context.Context) (<-chan int, error) {
respCounter := 0
if getAuthToken(ctx) == "authorized-user-token" {
respCounter = 1000
respChan := make(chan int, 1)
go func(respChan chan int) {
defer close(respChan)
counter := 0
for {
if counter == 10 {
return
}
respChan <- counter
counter++
time.Sleep(100 * time.Millisecond)
}
}(respChan)
return respChan, nil
}

func (s *subscriptionResolver) CountAuthorized(ctx context.Context) (<-chan int, error) {
if getAuthToken(ctx) != "authorized-user-token" {
return nil, fmt.Errorf("unauthorized")
}

respChan := make(chan int, 1)
go func(respChan chan int) {
defer close(respChan)
closeCounter := 0
counter := 0
for {
if closeCounter == 10 {
if counter == 10 {
return
}
closeCounter++
respChan <- respCounter
respCounter++
time.Sleep(100 * time.Millisecond)
respChan <- counter
counter++
}
}(respChan)
return respChan, nil
Expand Down

0 comments on commit 569efb2

Please sign in to comment.