From 9b918839b827a74ef40b8ac8b821a6b499875efd Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 11 Jul 2021 20:04:46 +0800 Subject: [PATCH] refactor: remove duplicated of init code. (#579) --- go.mod | 3 ++- go.sum | 9 +++++---- gorush/main_test.go | 33 +++++++++++++++++++++++++++++++++ gorush/notification_fcm_test.go | 19 ------------------- gorush/notification_hms_test.go | 19 ------------------- gorush/server_normal.go | 14 ++++++++++---- gorush/server_test.go | 32 ++++++++++++++++++++++++++++---- 7 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 gorush/main_test.go diff --git a/go.mod b/go.mod index f54412c..d86bf22 100644 --- a/go.mod +++ b/go.mod @@ -27,8 +27,9 @@ require ( github.com/thoas/stats v0.0.0-20190407194641-965cb2de1678 github.com/tidwall/buntdb v1.2.0 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 - golang.org/x/net v0.0.0-20210326220855-61e056675ecf + golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c + golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect google.golang.org/grpc v1.36.1 google.golang.org/protobuf v1.26.0 ) diff --git a/go.sum b/go.sum index bb975ce..8f5c96a 100644 --- a/go.sum +++ b/go.sum @@ -560,8 +560,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210326220855-61e056675ecf h1:WUcCxqQqDT0aXO4VnQbfMvp4zh7m1Gb2clVuHUAGGRE= -golang.org/x/net v0.0.0-20210326220855-61e056675ecf/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -610,8 +610,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg= -golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/gorush/main_test.go b/gorush/main_test.go new file mode 100644 index 0000000..3625880 --- /dev/null +++ b/gorush/main_test.go @@ -0,0 +1,33 @@ +package gorush + +import ( + "context" + "log" + "sync" + "testing" + + "github.com/appleboy/gorush/config" +) + +func TestMain(m *testing.M) { + PushConf, _ = config.LoadConf("") + if err := InitLog(); err != nil { + log.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + wg := &sync.WaitGroup{} + wg.Add(int(PushConf.Core.WorkerNum)) + InitWorkers(ctx, wg, PushConf.Core.WorkerNum, PushConf.Core.QueueNum) + + if err := InitAppStatus(); err != nil { + log.Fatal(err) + } + + defer func() { + close(QueueNotification) + cancel() + }() + + m.Run() +} diff --git a/gorush/notification_fcm_test.go b/gorush/notification_fcm_test.go index b9106e8..2740919 100644 --- a/gorush/notification_fcm_test.go +++ b/gorush/notification_fcm_test.go @@ -1,10 +1,7 @@ package gorush import ( - "context" - "log" "os" - "sync" "testing" "github.com/appleboy/go-fcm" @@ -12,22 +9,6 @@ import ( "github.com/stretchr/testify/assert" ) -func init() { - PushConf, _ = config.LoadConf("") - if err := InitLog(); err != nil { - log.Fatal(err) - } - - ctx := context.Background() - wg := &sync.WaitGroup{} - wg.Add(int(PushConf.Core.WorkerNum)) - InitWorkers(ctx, wg, PushConf.Core.WorkerNum, PushConf.Core.QueueNum) - - if err := InitAppStatus(); err != nil { - log.Fatal(err) - } -} - func TestMissingAndroidAPIKey(t *testing.T) { PushConf, _ = config.LoadConf("") diff --git a/gorush/notification_hms_test.go b/gorush/notification_hms_test.go index 81dcd34..d939dfb 100644 --- a/gorush/notification_hms_test.go +++ b/gorush/notification_hms_test.go @@ -1,31 +1,12 @@ package gorush import ( - "context" - "log" - "sync" "testing" "github.com/appleboy/gorush/config" "github.com/stretchr/testify/assert" ) -func init() { - PushConf, _ = config.LoadConf("") - if err := InitLog(); err != nil { - log.Fatal(err) - } - - ctx := context.Background() - wg := &sync.WaitGroup{} - wg.Add(int(PushConf.Core.WorkerNum)) - InitWorkers(ctx, wg, PushConf.Core.WorkerNum, PushConf.Core.QueueNum) - - if err := InitAppStatus(); err != nil { - log.Fatal(err) - } -} - func TestMissingHuaweiAppSecret(t *testing.T) { PushConf, _ = config.LoadConf("") diff --git a/gorush/server_normal.go b/gorush/server_normal.go index d596e5f..6b4660d 100644 --- a/gorush/server_normal.go +++ b/gorush/server_normal.go @@ -14,15 +14,21 @@ import ( ) // RunHTTPServer provide run http or https protocol. -func RunHTTPServer(ctx context.Context) (err error) { +func RunHTTPServer(ctx context.Context, s ...*http.Server) (err error) { + var server *http.Server + if !PushConf.Core.Enabled { LogAccess.Info("httpd server is disabled.") return nil } - server := &http.Server{ - Addr: PushConf.Core.Address + ":" + PushConf.Core.Port, - Handler: routerEngine(), + if len(s) == 0 { + server = &http.Server{ + Addr: PushConf.Core.Address + ":" + PushConf.Core.Port, + Handler: routerEngine(), + } + } else { + server = s[0] } LogAccess.Info("HTTPD server is running on " + PushConf.Core.Port + " port.") diff --git a/gorush/server_test.go b/gorush/server_test.go index b2769b9..c400fcc 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -63,8 +63,14 @@ func TestRunNormalServer(t *testing.T) { gin.SetMode(gin.TestMode) + ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(context.Background())) + assert.NoError(t, RunHTTPServer(ctx)) + }() + + defer func() { + // close the server + cancel() }() // have to wait for the goroutine to start and run the server // otherwise the main thread will complete @@ -81,8 +87,14 @@ func TestRunTLSServer(t *testing.T) { PushConf.Core.CertPath = "../certificate/localhost.cert" PushConf.Core.KeyPath = "../certificate/localhost.key" + ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(context.Background())) + assert.NoError(t, RunHTTPServer(ctx)) + }() + + defer func() { + // close the server + cancel() }() // have to wait for the goroutine to start and run the server // otherwise the main thread will complete @@ -103,8 +115,14 @@ func TestRunTLSBase64Server(t *testing.T) { PushConf.Core.CertBase64 = cert PushConf.Core.KeyBase64 = key + ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(context.Background())) + assert.NoError(t, RunHTTPServer(ctx)) + }() + + defer func() { + // close the server + cancel() }() // have to wait for the goroutine to start and run the server // otherwise the main thread will complete @@ -116,8 +134,14 @@ func TestRunTLSBase64Server(t *testing.T) { func TestRunAutoTLSServer(t *testing.T) { initTest() PushConf.Core.AutoTLS.Enabled = true + ctx, cancel := context.WithCancel(context.Background()) go func() { - assert.NoError(t, RunHTTPServer(context.Background())) + assert.NoError(t, RunHTTPServer(ctx)) + }() + + defer func() { + // close the server + cancel() }() // have to wait for the goroutine to start and run the server // otherwise the main thread will complete