-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathhttp_test.go
115 lines (99 loc) · 2.72 KB
/
http_test.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package server
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/go-dev-frame/sponge/pkg/servicerd/registry"
"github.com/go-dev-frame/sponge/pkg/utils"
"github.com/go-dev-frame/sponge/configs"
"github.com/go-dev-frame/sponge/internal/config"
)
// need real database to test
func TestHTTPServer(t *testing.T) {
err := config.Init(configs.Path("serverNameExample.yml"))
if err != nil {
t.Fatal(err)
}
config.Get().App.EnableMetrics = true
config.Get().App.EnableTrace = true
config.Get().App.EnableHTTPProfile = true
config.Get().App.EnableLimit = true
config.Get().App.EnableCircuitBreaker = true
port, _ := utils.GetAvailablePort()
addr := fmt.Sprintf(":%d", port)
gin.SetMode(gin.ReleaseMode)
utils.SafeRunWithTimeout(time.Second*2, func(cancel context.CancelFunc) {
server := NewHTTPServer(addr,
WithHTTPIsProd(true),
WithHTTPRegistry(&iRegistry{}, ®istry.ServiceInstance{}),
)
assert.NotNil(t, server)
cancel()
})
utils.SafeRunWithTimeout(time.Second, func(cancel context.CancelFunc) {
server := NewHTTPServer(addr)
assert.NotNil(t, server)
cancel()
})
utils.SafeRunWithTimeout(time.Second*2, func(cancel context.CancelFunc) {
server := NewHTTPServer_pbExample(addr,
WithHTTPIsProd(true),
WithHTTPRegistry(&iRegistry{}, ®istry.ServiceInstance{}),
)
assert.NotNil(t, server)
cancel()
})
utils.SafeRunWithTimeout(time.Second, func(cancel context.CancelFunc) {
server := NewHTTPServer_pbExample(addr)
assert.NotNil(t, server)
cancel()
})
}
func TestHTTPServerMock(t *testing.T) {
err := config.Init(configs.Path("serverNameExample.yml"))
if err != nil {
t.Fatal(err)
}
config.Get().App.EnableMetrics = true
config.Get().App.EnableTrace = true
config.Get().App.EnableHTTPProfile = true
config.Get().App.EnableLimit = true
config.Get().App.EnableCircuitBreaker = true
port, _ := utils.GetAvailablePort()
addr := fmt.Sprintf(":%d", port)
o := defaultHTTPOptions()
if o.isProd {
gin.SetMode(gin.ReleaseMode)
}
s := &httpServer{
addr: addr,
instance: ®istry.ServiceInstance{},
iRegistry: &iRegistry{},
}
s.server = &http.Server{
Addr: addr,
Handler: http.NewServeMux(),
MaxHeaderBytes: 1 << 20,
}
go func() {
time.Sleep(time.Second * 3)
_ = s.server.Shutdown(context.Background())
}()
str := s.String()
assert.NotEmpty(t, str)
err = s.Start()
assert.NoError(t, err)
err = s.Stop()
assert.NoError(t, err)
}
type iRegistry struct{}
func (i *iRegistry) Register(ctx context.Context, service *registry.ServiceInstance) error {
return nil
}
func (i *iRegistry) Deregister(ctx context.Context, service *registry.ServiceInstance) error {
return nil
}