Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BWbwchen committed Aug 8, 2021
1 parent cdf5754 commit d886b14
Show file tree
Hide file tree
Showing 47 changed files with 19,096 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ require (
github.com/google/go-cmp v0.5.6 // indirect
github.com/onsi/ginkgo v1.15.0 // indirect
github.com/onsi/gomega v1.10.5 // indirect
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.7.0
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
Expand Down
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"sync"

"github.com/gin-gonic/gin"
)
Expand All @@ -26,6 +27,10 @@ type GETresponse struct {
}

func main() {
r := setupRouter()
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.POST("/add", func(c *gin.Context) {
var body POSTData
Expand All @@ -39,11 +44,21 @@ func main() {
ShortName: shortName,
})
} else {
var wg sync.WaitGroup
wg.Add(2)
shortName := GetShortName(body.LongURL)
// add in database
DatabaseAdd(body.LongURL, shortName)
go func(LongURL string, shortName string) {
defer wg.Done()
DatabaseAdd(LongURL, shortName)
}(body.LongURL, shortName)
// add in redis
RedisAdd(body.LongURL, shortName)
go func(LongURL string, shortName string) {
defer wg.Done()
RedisAdd(LongURL, shortName)
}(body.LongURL, shortName)

wg.Wait()

c.JSON(http.StatusOK, POSTresponse{
Status: "OK",
Expand Down Expand Up @@ -78,5 +93,5 @@ func main() {
})
}
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
return r
}
79 changes: 79 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

type response struct {
Status string `json:"status"`
ShortName string `json:"short"`
URL string `json:"url"`
}

type testingSuite struct {
TestBed *testing.T
Router *gin.Engine
}

func TestShortURLService(t *testing.T) {
tb := testingSuite{
TestBed: t,
Router: setupRouter(),
}
for i := 0; i < 10; i++ {
originalURL := "https://" + randomString(30)
shortURL := tb.requestShortURL(originalURL)
URL := tb.requestLongURL(shortURL)
assert.Equal(t, originalURL, URL)
}

}
func (tb testingSuite) requestShortURL(originalURL string) string {
gin.SetMode(gin.TestMode)
postBody, _ := json.Marshal(map[string]string{
"url": originalURL,
})
requestBody := bytes.NewBuffer(postBody)
req, _ := http.NewRequest("POST", "/add", requestBody)
req.Header.Add("Content-Type", "application/json")

resp := httptest.NewRecorder()
tb.Router.ServeHTTP(resp, req)
assert.Equal(tb.TestBed, 200, resp.Code)

body, _ := ioutil.ReadAll(resp.Body)
var respBody response
json.Unmarshal(body, &respBody)

return respBody.ShortName
}

func (tb testingSuite) requestLongURL(shortURL string) string {
// get
req, _ := http.NewRequest("GET", "/"+shortURL, nil)

resp := httptest.NewRecorder()
tb.Router.ServeHTTP(resp, req)
assert.Equal(tb.TestBed, 200, resp.Code)
body, _ := ioutil.ReadAll(resp.Body)
var respBody response
json.Unmarshal(body, &respBody)
return respBody.URL
}
func randomString(length int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, length)
rand.Read(b)
return fmt.Sprintf("%x", b)[:length]
}
11 changes: 7 additions & 4 deletions performance_test/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { check, group, sleep } from 'k6';

export let options = {
stages: [
{ duration: '15s', target: 20 }, // simulate ramp-up of traffic from 1 to 100 users over 5 minutes.
{ duration: '1m', target: 20 }, // stay at 100 users for 10 minutes
{ duration: '15s', target: 0 }, // ramp-down to 0 users
{ duration: '1s', target: 30 }, // stay at 100 users for 10 minutes
{ duration: '20s', target: 30 }, // stay at 100 users for 10 minutes
{ duration: '1s', target: 0 }, // stay at 100 users for 10 minutes
]
};

const BASE_URL = 'http://192.168.0.201:30390';
//const BASE_URL = 'http://192.168.0.201:30390';
const BASE_URL = 'http://192.168.0.161:8081';
//const BASE_URL = 'http://192.168.0.226';
//const BASE_URL = 'http://shorturl.bw';

function uuidv4() {
return 'http://xxxxx4xxxyx.xxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
Expand Down
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

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

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

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

38 changes: 38 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

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

Loading

0 comments on commit d886b14

Please sign in to comment.