Skip to content

Commit a420d7c

Browse files
feat(ci): add ci workflow (#17)
* feat(ci): add ci workflow * chore(ci): update lint action version. * chore(ci): bump actions version. * chore(ci): increase lint step timeout. * test: add test case name. * test: add executeAfter * test: add executeAfter
1 parent 7182747 commit a420d7c

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

.github/workflows/ci.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Lint and Test
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
golangci:
9+
name: Lint
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/setup-go@v3
13+
with:
14+
go-version: '1.17'
15+
16+
- uses: actions/checkout@v3
17+
18+
- uses: golangci/golangci-lint-action@v3
19+
with:
20+
version: latest
21+
args: --timeout=3m
22+
23+
test:
24+
name: Test
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/setup-go@v3
28+
with:
29+
go-version: '1.17'
30+
31+
- uses: actions/checkout@v3
32+
33+
- uses: actions/cache@v3
34+
with:
35+
path: |
36+
~/go/pkg/mod # Module download cache
37+
~/.cache/go-build # Build cache (Linux)
38+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
39+
restore-keys: |
40+
${{ runner.os }}-go-
41+
42+
- run: go get
43+
44+
- run: go test -race -covermode atomic -coverprofile=coverage.txt -coverpkg=github.com/gbox-proxy/gbox ./...
45+
46+
- uses: codecov/codecov-action@v3
47+
with:
48+
path-to-profile: coverage.txt

handler_test.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ const (
2626
http_port 9090
2727
https_port 9443
2828
}
29-
localhost:9090 {
30-
}
3129
`
3230
caddyfilePattern = `
3331
{
@@ -382,7 +380,7 @@ caching {
382380
tester.InitServer(fmt.Sprintf(caddyfilePattern, config), "caddyfile")
383381

384382
for _, testCase := range testCases {
385-
<-time.After(testCase.executeAfter)
383+
<-time.After(testCase.executeAfter) // wait for caching in background
386384

387385
r, _ := http.NewRequest(
388386
"POST",
@@ -410,6 +408,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingEnabledAutoInvalidate() {
410408
const mutationPayload = `{"query": "mutation InvalidateUsers { updateUsers { id } }"}`
411409
testCases := []struct {
412410
name string
411+
executeAfter time.Duration
413412
expectedHitTimes string
414413
expectedCachingStatus CachingStatus
415414
expectedBody string
@@ -430,6 +429,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingEnabledAutoInvalidate() {
430429
expectedHitTimes: "1",
431430
payload: payloadNameOnly,
432431
expectedCachingTags: `field:QueryTest:users, field:UserTest:name, operation:UsersNameOnly, schema:4230843191964202593, type:QueryTest, type:UserTest`,
432+
executeAfter: time.Millisecond * 5,
433433
},
434434
{
435435
name: "type_keys_miss_on_first_time",
@@ -444,6 +444,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingEnabledAutoInvalidate() {
444444
expectedHitTimes: "1",
445445
payload: payload,
446446
expectedCachingTags: `field:QueryTest:users, field:UserTest:id, field:UserTest:name, key:UserTest:id:1, key:UserTest:id:2, key:UserTest:id:3, operation:Users, schema:4230843191964202593, type:QueryTest, type:UserTest`,
447+
executeAfter: time.Millisecond * 5,
447448
},
448449
{
449450
name: "mutation_invalidate_query_result",
@@ -483,6 +484,8 @@ caching {
483484
tester.InitServer(fmt.Sprintf(caddyfilePattern, config), "caddyfile")
484485

485486
for _, testCase := range testCases {
487+
<-time.After(testCase.executeAfter) // wait for caching in background
488+
486489
r, _ := http.NewRequest(
487490
"POST",
488491
"http://localhost:9090/graphql",
@@ -512,6 +515,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingDisabledAutoInvalidate() {
512515
const mutationPayload = `{"query": "mutation InvalidateUsers { updateUsers { id } }"}`
513516
testCases := []struct {
514517
name string
518+
executeAfter time.Duration
515519
expectedHitTimes string
516520
expectedCachingStatus CachingStatus
517521
expectedBody string
@@ -530,6 +534,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingDisabledAutoInvalidate() {
530534
expectedBody: `{"data":{"users":[{"id":1,"name":"A"},{"id":2,"name":"B"},{"id":3,"name":"C"}]}}`,
531535
expectedHitTimes: "1",
532536
payload: payload,
537+
executeAfter: time.Millisecond * 5,
533538
},
534539
{
535540
name: "mutation_invalidated_query_result_disabled",
@@ -562,6 +567,8 @@ caching {
562567
tester.InitServer(fmt.Sprintf(caddyfilePattern, config), "caddyfile")
563568

564569
for _, testCase := range testCases {
570+
<-time.After(testCase.executeAfter) // wait for caching in background
571+
565572
r, _ := http.NewRequest(
566573
"POST",
567574
"http://localhost:9090/graphql",
@@ -587,6 +594,7 @@ caching {
587594
func (s *HandlerIntegrationTestSuite) TestCachingVaries() {
588595
testCases := []struct {
589596
name string
597+
executeAfter time.Duration
590598
expectedHitTimes string
591599
expectedCachingStatus CachingStatus
592600
expectedBody string
@@ -605,6 +613,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingVaries() {
605613
expectedCachingStatus: CachingStatusHit,
606614
expectedBody: `{"data":{"users":[{"name":"A"},{"name":"B"},{"name":"C"}]}}`,
607615
expectedHitTimes: "1",
616+
executeAfter: time.Millisecond * 5,
608617
},
609618
{
610619
name: "miss_on_difference_vary_headers",
@@ -632,6 +641,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingVaries() {
632641
"x-test": "1",
633642
},
634643
},
644+
executeAfter: time.Millisecond * 5,
635645
},
636646
{
637647
name: "miss_on_difference_vary_cookies",
@@ -659,6 +669,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingVaries() {
659669
"x-test": "1",
660670
},
661671
},
672+
executeAfter: time.Millisecond * 5,
662673
},
663674
{
664675
name: "miss_on_difference_vary_headers_cookies",
@@ -692,6 +703,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingVaries() {
692703
"x-test": "2",
693704
},
694705
},
706+
executeAfter: time.Millisecond * 5,
695707
},
696708
}
697709

@@ -718,6 +730,8 @@ caching {
718730
tester.InitServer(fmt.Sprintf(caddyfilePattern, config), "caddyfile")
719731

720732
for _, testCase := range testCases {
733+
<-time.After(testCase.executeAfter) // wait for caching in background
734+
721735
r, _ := http.NewRequest(
722736
"POST",
723737
"http://localhost:9090/graphql",
@@ -944,14 +958,15 @@ caching {
944958
body, _ := io.ReadAll(resp.Body)
945959
resp.Body.Close()
946960

947-
s.Require().Equal(string(body), `{"data":{"users":[{"id":1,"name":"A"},{"id":2,"name":"B"},{"id":3,"name":"C"}]}}`, "unexpected response")
961+
s.Require().Equalf(string(body), `{"data":{"users":[{"id":1,"name":"A"},{"id":2,"name":"B"},{"id":3,"name":"C"}]}}`, "case %s: unexpected response", testCase.name)
948962

949963
if i == 0 {
950964
// always miss on first time.
951-
s.Require().Equal(string(CachingStatusMiss), resp.Header.Get("x-cache"), "cache status must MISS on first time")
965+
s.Require().Equalf(string(CachingStatusMiss), resp.Header.Get("x-cache"), "case %s: cache status must MISS on first time", testCase.name)
952966
} else {
953-
s.Require().Equal(string(CachingStatusHit), resp.Header.Get("x-cache"), "cache status must HIT on next time")
954-
s.Require().Equal(strconv.Itoa(i), resp.Header.Get("x-cache-hits"), "hit times not equal")
967+
<-time.After(time.Millisecond * 5) // wait for caching in background
968+
s.Require().Equalf(string(CachingStatusHit), resp.Header.Get("x-cache"), "case %s: cache status must HIT on next time", testCase.name)
969+
s.Require().Equalf(strconv.Itoa(i), resp.Header.Get("x-cache-hits"), "case %s: hit times not equal", testCase.name)
955970
}
956971
}
957972

@@ -980,7 +995,7 @@ func (s *HandlerIntegrationTestSuite) TestCachingControlRequestHeader() {
980995
name string
981996
cc string
982997
expectedCachingStatus CachingStatus
983-
waitTime time.Duration
998+
executeAfter time.Duration
984999
}{
9851000
{
9861001
name: "first_time_cc_no_store",
@@ -1004,49 +1019,49 @@ func (s *HandlerIntegrationTestSuite) TestCachingControlRequestHeader() {
10041019
},
10051020
{
10061021
name: "max_age_with_valid_max_stale_cc_result_stale_still_hit",
1007-
waitTime: time.Millisecond * 50,
1022+
executeAfter: time.Millisecond * 50,
10081023
cc: "max-age=1, max-stale=2",
10091024
expectedCachingStatus: CachingStatusHit,
10101025
},
10111026
{
10121027
name: "max_age_with_empty_max_stale_cc_result_stale_still_hit",
1013-
waitTime: time.Millisecond * 50,
1028+
executeAfter: time.Millisecond * 50,
10141029
cc: "max-age=1, max-stale",
10151030
expectedCachingStatus: CachingStatusHit,
10161031
},
10171032
{
10181033
name: "max_age_with_invalid_max_stale_cc_result_stale_will_miss",
1019-
waitTime: time.Millisecond * 2050,
1034+
executeAfter: time.Millisecond * 2050,
10201035
cc: "max-age=1, max-stale=1",
10211036
expectedCachingStatus: CachingStatusMiss,
10221037
},
10231038
{
10241039
name: "max_age_without_max_stale_cc_result_stale_will_miss",
1025-
waitTime: time.Millisecond * 50,
1040+
executeAfter: time.Millisecond * 50,
10261041
cc: "max-age=1",
10271042
expectedCachingStatus: CachingStatusMiss,
10281043
},
10291044
{
10301045
name: "invalid_min_fresh_cc_will_miss",
1031-
waitTime: time.Second,
1046+
executeAfter: time.Second,
10321047
cc: "min-fresh=1",
10331048
expectedCachingStatus: CachingStatusMiss,
10341049
},
10351050
{
10361051
name: "invalid_max_stale_cc_will_miss",
1037-
waitTime: time.Millisecond * 1050,
1052+
executeAfter: time.Millisecond * 1050,
10381053
cc: "max-stale=1",
10391054
expectedCachingStatus: CachingStatusMiss,
10401055
},
10411056
{
10421057
name: "empty_max_stale_cc_will_hit",
1043-
waitTime: time.Millisecond * 55,
1058+
executeAfter: time.Millisecond * 55,
10441059
cc: "max-stale",
10451060
expectedCachingStatus: CachingStatusHit,
10461061
},
10471062
{
10481063
name: "valid_max_stale_cc_will_hit",
1049-
waitTime: time.Millisecond * 55,
1064+
executeAfter: time.Millisecond * 55,
10501065
cc: "max-stale=1",
10511066
expectedCachingStatus: CachingStatusHit,
10521067
},
@@ -1065,7 +1080,7 @@ caching {
10651080
`), "caddyfile")
10661081

10671082
for _, testCase := range testCases {
1068-
<-time.After(testCase.waitTime)
1083+
<-time.After(testCase.executeAfter) // wait for caching in background
10691084

10701085
r, _ := http.NewRequest(
10711086
"POST",

0 commit comments

Comments
 (0)