Skip to content

Commit 3a5e637

Browse files
committed
starting test server using background-action
1 parent ac07be0 commit 3a5e637

File tree

6 files changed

+61
-36
lines changed

6 files changed

+61
-36
lines changed

.github/workflows/tests.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212
- "*"
1313

1414
jobs:
15-
RunTest:
15+
RunTests:
1616
runs-on: ubuntu-latest
1717
steps:
1818
- uses: actions/checkout@v4
@@ -24,11 +24,26 @@ jobs:
2424
cache-dependency-path: |
2525
go.sum
2626
27-
- name: Launch Test Server (todo)
28-
run: go run test_api/todo/server.go & sleep 5
27+
- name: Start Todo API
28+
uses: JarvusInnovations/background-action@v1
29+
with:
30+
run: go run test_api/todo/server.go
31+
wait-on: |
32+
http://localhost:8080
33+
tail: true
34+
wait-for: 30s
35+
log-output-if: failure
2936

30-
- name: Launch Cache Server
31-
run: go run main.go & sleep 5
37+
- name: Start Cache API
38+
uses: JarvusInnovations/background-action@v1
39+
with:
40+
run: go run main.go
41+
wait-on: |
42+
http://localhost:9090
43+
tail: true
44+
wait-for: 30s
45+
log-output-if: failure
3246

33-
- name: Run Test
34-
run: go test -v ./...
47+
- name: Run Tests
48+
run: |
49+
go test -v ./...

api/handlers/cache_handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
7373
client := http.Client{}
7474
// Send the proxy request using the custom transport
7575
resp, err := client.Do(proxyReq)
76-
if err != nil {
76+
if err != nil || resp == nil {
7777
http.Error(w, "Error sending proxy request", http.StatusInternalServerError)
7878

7979
}
@@ -98,7 +98,7 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
9898
responseMap := make(map[string]interface{})
9999
err = json.Unmarshal(responseBody.Bytes(), &responseMap)
100100
if err != nil {
101-
log.Println("Error unmarshalling response:", err)
101+
log.Println("Error unmarshalling response:", err, string(responseBody.Bytes()))
102102
}
103103

104104
cache.InvalidateCache("data", responseMap, nil)
@@ -148,7 +148,7 @@ func GetCacheHandler(cache *graphcache.GraphCache, cfg *config.Config) http.Hand
148148
responseMap := make(map[string]interface{})
149149
err = json.Unmarshal(responseBody.Bytes(), &responseMap)
150150
if err != nil {
151-
log.Println("Error unmarshalling response:", err)
151+
log.Println("Error unmarshalling response:", err, string(responseBody.Bytes()))
152152
}
153153

154154
log.Println("time taken to get response from API ", time.Since(start))

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module graphql_cache
22

3-
go 1.20
3+
go 1.22
4+
5+
toolchain go1.22.5
46

57
require (
68
github.com/99designs/gqlgen v0.17.49
@@ -18,13 +20,15 @@ require (
1820

1921
require (
2022
github.com/agnivade/levenshtein v1.1.1 // indirect
23+
github.com/bitnami/wait-for-port v1.0.8 // indirect
2124
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2225
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
2326
github.com/davecgh/go-spew v1.1.1 // indirect
2427
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2528
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
2629
github.com/gorilla/websocket v1.5.0 // indirect
2730
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
31+
github.com/jessevdk/go-flags v1.6.1 // indirect
2832
github.com/jinzhu/inflection v1.0.0 // indirect
2933
github.com/jinzhu/now v1.1.5 // indirect
3034
github.com/kr/pretty v0.1.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo
1111
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
1212
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
1313
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
14+
github.com/bitnami/wait-for-port v1.0.8 h1:x2538PUfe99r41GMCaSOVY14JPhIGoIiHCJ/i+paFtM=
15+
github.com/bitnami/wait-for-port v1.0.8/go.mod h1:IOscZQFlU1WOxWDCyBk7ys+Hy3LaNRqNJXyAu9vzyaE=
1416
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
1517
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
1618
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
@@ -33,6 +35,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
3335
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3436
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
3537
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
38+
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
39+
github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc=
3640
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
3741
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
3842
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=

graphcache/graphcache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (gc *GraphCache) RemoveTypenameFromResponse(response *GraphQLResponse) (*Gr
4747
}
4848
err = json.Unmarshal(responseBytes, &mapResponse)
4949
if err != nil {
50-
fmt.Println("Error unmarshalling response:", err)
50+
fmt.Println("Error unmarshalling response: ", err, string(responseBytes))
5151
return nil, err
5252
}
5353

@@ -62,7 +62,7 @@ func (gc *GraphCache) RemoveTypenameFromResponse(response *GraphQLResponse) (*Gr
6262
gres := GraphQLResponse{}
6363
err = json.Unmarshal(br, &gres)
6464
if err != nil {
65-
fmt.Println("Error unmarshalling response:", err)
65+
fmt.Println("Error unmarshalling response:", err, string(br))
6666
return nil, err
6767
}
6868

main_test.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212

1313
const NUMBER_OF_USERS = 10
1414

15-
func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Duration {
15+
func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) (time.Duration, error) {
1616
totalTimeTaken := time.Duration(0)
1717

1818
deleted, tt, err := client.DeleteEverything()
1919
if err != nil {
20-
fmt.Println(err)
21-
return totalTimeTaken
20+
fmt.Println("error resetting database ", err)
21+
return totalTimeTaken, err
2222
}
2323

2424
totalTimeTaken += tt
@@ -33,8 +33,8 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Du
3333
for i := 0; i < NUMBER_OF_USERS; i++ {
3434
user, tt, err := client.CreateRandomUser()
3535
if err != nil {
36-
fmt.Println(err)
37-
return totalTimeTaken
36+
fmt.Println("error creating user ", err)
37+
return totalTimeTaken, err
3838
}
3939
totalTimeTaken += tt
4040
userIDToUpdate = user.ID.String()
@@ -47,8 +47,8 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Du
4747
for i := 0; i < NUMBER_OF_USERS; i++ {
4848
users, tt, err := client.PaginateUsers()
4949
if err != nil {
50-
fmt.Println(err)
51-
return totalTimeTaken
50+
fmt.Println("error paginating users ", err)
51+
return totalTimeTaken, err
5252
}
5353
totalTimeTaken += tt
5454

@@ -58,17 +58,17 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Du
5858

5959
user, tt, err := client.CreateRandomUser()
6060
if err != nil {
61-
fmt.Println(err)
62-
return totalTimeTaken
61+
fmt.Println("error creating user ", err)
62+
return totalTimeTaken, err
6363
}
6464
totalTimeTaken += tt
6565

6666
client.FlushByType("User", "")
6767

6868
users, tt, err := client.PaginateUsers()
6969
if err != nil {
70-
fmt.Println(err)
71-
return totalTimeTaken
70+
fmt.Println("error paginating users ", err)
71+
return totalTimeTaken, err
7272
}
7373
totalTimeTaken += tt
7474

@@ -78,8 +78,8 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Du
7878

7979
user, tt, err = client.GetUserByID(userIDToUpdate)
8080
if err != nil {
81-
fmt.Println(err)
82-
return totalTimeTaken
81+
fmt.Println("error fetching user", err)
82+
return totalTimeTaken, err
8383
}
8484
totalTimeTaken += tt
8585

@@ -90,8 +90,8 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Du
9090
// Update a user
9191
user, tt, err = client.UpdateUser(userIDToUpdate, "Updated Name", "", "")
9292
if err != nil {
93-
fmt.Println(err)
94-
return totalTimeTaken
93+
fmt.Println("error updating user ", err)
94+
return totalTimeTaken, err
9595
}
9696
totalTimeTaken += tt
9797

@@ -100,31 +100,33 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) time.Du
100100
for i := 0; i < NUMBER_OF_USERS; i++ {
101101
updatedUser, tt, err := client.GetUserByID(userIDToUpdate)
102102
if err != nil {
103-
fmt.Println(err)
104-
return totalTimeTaken
103+
fmt.Println("error fetching user ", err)
104+
return totalTimeTaken, err
105105
}
106106
totalTimeTaken += tt
107107

108108
assert.Equal(t, "Updated Name", updatedUser.Name)
109109
}
110110

111-
return totalTimeTaken
111+
return totalTimeTaken, nil
112112

113113
}
114114

115115
func TestAPICacheTestSuite(t *testing.T) {
116116
client := test_client.NewGraphQLClient("http://localhost:9090/graphql", "http://localhost:9090")
117117
defer client.DeleteEverything()
118118
defer client.FlushCache()
119-
timeTaken := RunUsersOperations(t, client)
119+
timeTaken, err := RunUsersOperations(t, client)
120+
assert.Nil(t, err)
120121
fmt.Printf("Total time taken for Cached Todo API: %v\n", timeTaken)
121122
}
122123

123124
func TestAPIDefaultTestSuite(t *testing.T) {
124125
client := test_client.NewGraphQLClient("http://localhost:8080/graphql", "")
125126
defer client.DeleteEverything()
126127
defer client.FlushCache()
127-
timeTaken := RunUsersOperations(t, client)
128+
timeTaken, err := RunUsersOperations(t, client)
129+
assert.Nil(t, err)
128130
fmt.Printf("Total time taken for Default Todo API: %v\n", timeTaken)
129131
}
130132

@@ -142,7 +144,7 @@ func TestAPIResponseConsistency(t *testing.T) {
142144
for i := 0; i < NUMBER_OF_USERS; i++ {
143145
user, _, err := cacheClient.CreateRandomUser()
144146
if err != nil {
145-
fmt.Println(err)
147+
fmt.Println("error ", err)
146148
return
147149
}
148150
createdUsers = append(createdUsers, *user)
@@ -155,7 +157,7 @@ func TestAPIResponseConsistency(t *testing.T) {
155157
for i := 0; i < NUMBER_OF_USERS; i++ {
156158
users, tt, err := cacheClient.PaginateUsers()
157159
if err != nil {
158-
fmt.Println(err)
160+
fmt.Println("error ", err)
159161
return
160162
}
161163
cachedUsersResponses = append(cachedUsersResponses, users...)
@@ -167,7 +169,7 @@ func TestAPIResponseConsistency(t *testing.T) {
167169
for i := 0; i < NUMBER_OF_USERS; i++ {
168170
users, tt, err := defaultClient.PaginateUsers()
169171
if err != nil {
170-
fmt.Println(err)
172+
fmt.Println("error ", err)
171173
return
172174
}
173175
defaultUserResponses = append(defaultUserResponses, users...)

0 commit comments

Comments
 (0)