-
Notifications
You must be signed in to change notification settings - Fork 50
/
responsetime.go
42 lines (34 loc) · 971 Bytes
/
responsetime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package app
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/go-logr/logr"
)
//counterfeiter:generate . TimeWaster
type TimeWaster interface {
Sleep(sleepTime time.Duration)
}
type Sleeper struct{}
var _ TimeWaster = Sleeper{}
func ResponseTimeTests(logger logr.Logger, r *gin.RouterGroup, timeWaster TimeWaster) *gin.RouterGroup {
r.GET("/slow/:delayInMS", func(c *gin.Context) {
var milliseconds uint64
var err error
if milliseconds, err = strconv.ParseUint(c.Param("delayInMS"), 10, 64); err != nil {
Error(c, http.StatusBadRequest, "invalid milliseconds: %s", err.Error())
return
}
duration := time.Duration(milliseconds) * time.Millisecond
timeWaster.Sleep(duration)
c.JSON(http.StatusOK, gin.H{"duration": duration.String()})
})
r.GET("/fast", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"fast": true})
})
return r
}
func (Sleeper) Sleep(sleepTime time.Duration) {
time.Sleep(sleepTime)
}