Skip to content

Commit

Permalink
Merge pull request #5 from HatsuneMiku3939/test/add-basic-test
Browse files Browse the repository at this point in the history
test: add basic test code
  • Loading branch information
HatsuneMiku3939 authored May 17, 2022
2 parents 03fd98f + 15ee93d commit fcc1591
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 13 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Unit Test

env:
GOVERSION: '1.17'
GOMOD_CACHE_NAME: 'go-cache'

on:
pull_request:
Expand All @@ -23,16 +24,20 @@ jobs:
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
key: ${{ runner.os }}-${{ env.GOMOD_CACHE_NAME }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
${{ runner.os }}-${{ env.GOMOD_CACHE_NAME }}-
- name: Download go module
run: go mod download

- name: Download go module for test
run: cd test && go mod download

- name: Build tools for go module caching
run: make tools


test:
name: Unit Testing
runs-on: ubuntu-latest
Expand All @@ -50,9 +55,9 @@ jobs:
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
key: ${{ runner.os }}-${{ env.GOMOD_CACHE_NAME }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
${{ runner.os }}-${{ env.GOMOD_CACHE_NAME }}-
- name: Test
run: make test
Expand All @@ -74,9 +79,9 @@ jobs:
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
key: ${{ runner.os }}-${{ env.GOMOD_CACHE_NAME }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
${{ runner.os }}-${{ env.GOMOD_CACHE_NAME }}-
- name: Lint
run: make lint-ci
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ lint: $(TOOLS_DIR)/golangci-lint
.PHONY: test
test:
@echo TEST
@go test ./...
@cd test && go test -v ./...
@printf "TEST... \033[0;32m [OK] \033[0m\n"

## test/coverage: Run test and generate coverage report
.PHONY: test/coverage
test/coverage:
@go test ./... -coverprofile=coverage.txt -covermode=atomic
@go tool cover -html=coverage.txt -o coverage.html

.PHONY: $(BIN_DIR)
$(BIN_DIR):
@mkdir -p $@
Expand Down
16 changes: 16 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/HatsuneMiku3939/rltransport/test

go 1.17

require (
github.com/HatsuneMiku3939/rltransport v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.7.1
)

replace github.com/HatsuneMiku3939/rltransport => ../

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
11 changes: 11 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29 changes: 29 additions & 0 deletions test/monitering_tripper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test

import (
"net/http"
"net/http/httptest"
)

type MoniteringTripper struct {
TripCount int
}

func NewMoniteringTripper() *MoniteringTripper {
return &MoniteringTripper{
TripCount: 0,
}
}

func (t *MoniteringTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
w.WriteHeader(200)
w.Write([]byte("OK"))
t.TripCount++

return w.Result(), nil
}

func (t *MoniteringTripper) Reset() {
t.TripCount = 0
}
120 changes: 120 additions & 0 deletions test/roundtripper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package test

import (
"net/http"
"testing"

"github.com/HatsuneMiku3939/rltransport"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

// RoundTripperTestSuite is a testify suite for RoundTripper
type RounterTripperTestSuite struct {
suite.Suite
}

func (s *RounterTripperTestSuite) TestUnlimited() {
// dummy RoundTripper for test
monitor := NewMoniteringTripper()

// Create a new http client with unlimited limit
client := &http.Client{
Transport: &rltransport.RoundTripper{
Transport: monitor,
},
}

// test cases
cases := []struct {
sendCount int
want int
}{
{
sendCount: 0,
want: 0,
},
{
sendCount: 1,
want: 1,
},
{
sendCount: 100000,
want: 100000,
},
}

for _, c := range cases {
monitor.Reset()

// Send requests
for i := 0; i < c.sendCount; i++ {
client.Get("http://example.com")
}

// Check the result
assert.Equalf(s.T(), c.want, monitor.TripCount, "sendCount: %d want %d", c.sendCount, c.want)
}
}

// TestSimpleLimiter is a test for simple limiter
func (s *RounterTripperTestSuite) TestSimpleLimiter() {
// dummy RoundTripper for test
monitor := NewMoniteringTripper()

// simple limiter for test
limiter := &simpleLimiter{
Count: 0,
Limit: 100,
}

// Create a new http client with limiter
client := &http.Client{
Transport: &rltransport.RoundTripper{
Transport: monitor,
Limiter: limiter,
},
}

// test cases
cases := []struct {
sendCount int
want int
}{
{
sendCount: 0,
want: 0,
},
{
sendCount: 1,
want: 1,
},
// send 100000 requests, limited to 100 by simple limiter
{
sendCount: 100000,
want: limiter.Limit,
},
}

for _, c := range cases {
failCount := 0
monitor.Reset()
limiter.Reset()

// Send requests
for i := 0; i < c.sendCount; i++ {
_, err := client.Get("http://example.com")
if err != nil {
failCount++
}
}

// Check the result
assert.Equalf(s.T(), c.want, monitor.TripCount, "sendCount: %d want %d", c.sendCount, c.want)
}
}

func TestRounterTripper(t *testing.T) {
suite.Run(t, new(RounterTripperTestSuite))
}
24 changes: 24 additions & 0 deletions test/simple_limiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test

import (
"context"
"fmt"
)

type simpleLimiter struct {
Count int
Limit int
}

func (l *simpleLimiter) Wait(ctx context.Context) error {
if l.Count >= l.Limit {
return fmt.Errorf("limit exceeded")
}

l.Count++
return nil
}

func (l *simpleLimiter) Reset() {
l.Count = 0
}

0 comments on commit fcc1591

Please sign in to comment.