oceanbase

Форк
0
/
http_server_test.go 
93 строки · 2.3 Кб
1
/**
2
 * Copyright (c) 2021 OceanBase
3
 * OceanBase CE is licensed under Mulan PubL v2.
4
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
5
 * You may obtain a copy of Mulan PubL v2 at:
6
 *          http://license.coscl.org.cn/MulanPubL-2.0
7
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
 * See the Mulan PubL v2 for more details.
11
 */
12

13
package server
14

15
import (
16
	"context"
17
	"net/http"
18
	"net/http/httptest"
19
	"testing"
20
	"time"
21

22
	"github.com/gin-gonic/gin"
23
	. "github.com/smartystreets/goconvey/convey"
24
)
25

26
func TestCounter(t *testing.T) {
27
	count := new(Counter)
28
	Convey("counter after init", t, func() {
29
		So(count.sessionCount, ShouldEqual, 0)
30
	})
31

32
	count.incr()
33
	Convey("counter after incr", t, func() {
34
		So(count.sessionCount, ShouldEqual, 1)
35
	})
36

37
	count.decr()
38
	Convey("counter after decr", t, func() {
39
		So(count.sessionCount, ShouldEqual, 0)
40
	})
41
}
42

43
func TestHttpServer(t *testing.T) {
44
	server := &HttpServer{
45
		Counter: new(Counter),
46
		Router:  gin.Default(),
47
		Server: &http.Server{
48
			Addr: ":0",
49
		},
50
	}
51

52
	w := httptest.NewRecorder()
53
	server.UseCounter()
54
	server.Router.GET("/foo", fooHandler)
55
	end := make(chan bool, 1)
56
	handler := func(w http.ResponseWriter, r *http.Request) {
57
		server.Router.ServeHTTP(w, r)
58
		time.Sleep(time.Second)
59
	}
60
	req := httptest.NewRequest(http.MethodGet, "/foo", nil)
61
	go func() {
62
		handler(w, req)
63
		end <- true
64
	}()
65

66
	time.Sleep(10 * time.Millisecond)
67
	t.Run("handle a 1 second request", func(t *testing.T) {
68
		Convey("session count should be 1", t, func() {
69
			So(server.Counter.sessionCount, ShouldEqual, 1)
70
		})
71

72
		err := server.Shutdown(context.Background())
73
		Convey("server shutdown should fail", t, func() {
74
			So(err, ShouldNotBeNil)
75
			So(err.Error(), ShouldContainSubstring, "server shutdown failed")
76
		})
77
	})
78

79
	<-end
80
	t.Run("handle request end", func(t *testing.T) {
81
		Convey("session count should be 0", t, func() {
82
			So(server.Counter.sessionCount, ShouldEqual, 0)
83
		})
84
		err := server.Shutdown(context.Background())
85
		Convey("server shutdown should success", t, func() {
86
			So(err, ShouldBeNil)
87
		})
88
	})
89
}
90

91
func fooHandler(c *gin.Context) {
92
	time.Sleep(time.Second)
93
}
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.