refactor: remove duplicated of init code. (#579)

This commit is contained in:
Bo-Yi Wu 2021-07-11 20:04:46 +08:00 committed by GitHub
parent 05bc48cba6
commit 9b918839b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 51 deletions

3
go.mod
View File

@ -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
)

9
go.sum
View File

@ -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=

33
gorush/main_test.go Normal file
View File

@ -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()
}

View File

@ -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("")

View File

@ -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("")

View File

@ -14,16 +14,22 @@ 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{
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.")
if PushConf.Core.AutoTLS.Enabled {

View File

@ -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