Skip to content

Commit a9cf548

Browse files
committed
move app startup to server pkg
improve test coverage
1 parent 61ff44c commit a9cf548

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

app.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package main
33
import (
44
"flag"
55

6-
"github.com/mch1307/gomotics/log"
7-
8-
"github.com/mch1307/gomotics/config"
96
"github.com/mch1307/gomotics/server"
107
)
118

@@ -19,10 +16,10 @@ func init() {
1916

2017
func main() {
2118
flag.Parse()
22-
Sub(conf)
19+
server.Start(conf)
2320
}
2421

25-
// Sub actually starts the servers
22+
/* // Sub actually starts the servers
2623
func Sub(conf string) {
2724
config.Initialize(conf)
2825
log.Init()
@@ -31,3 +28,4 @@ func Sub(conf string) {
3128
s.Run()
3229
server.NhcInit(&config.Conf.NhcConfig)
3330
}
31+
*/

server/server.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/gorilla/mux"
1111
"github.com/mch1307/gomotics/config"
1212
"github.com/mch1307/gomotics/log"
13+
//"github.com/mch1307/gomotics/server"
1314
)
1415

1516
// HealthMsg static alive json for health endpoint
@@ -25,8 +26,14 @@ type Server struct {
2526
LogFile string
2627
}
2728

28-
func init() {
29-
//log.Init()
29+
// Start Initialize and starts the server
30+
func Start(conf string) {
31+
config.Initialize(conf)
32+
log.Init()
33+
s := Server{}
34+
s.Initialize()
35+
s.Run()
36+
NhcInit(&config.Conf.NhcConfig)
3037
fmt.Println("Starting gomotics")
3138
}
3239

server/server_test.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/http/httptest"
1313
"os"
1414
"reflect"
15-
"strconv"
1615
"testing"
1716
"time"
1817

@@ -128,24 +127,33 @@ func InitStubNHC() {
128127

129128
if IsTCPPortAvailable(8081) && IsTCPPortAvailable(8000) {
130129
fmt.Println("starting InitStubNHC")
131-
config.Conf.NhcConfig.Host = ConnectHost
132-
config.Conf.NhcConfig.Port, _ = strconv.Atoi(ConnectPort)
133-
config.Conf.ServerConfig.ListenPort = 8081
134-
config.Conf.ServerConfig.LogLevel = "DEBUG"
135130
go MockNHC()
136-
go NhcListener()
137-
time.Sleep(1000 * time.Millisecond)
131+
go stubNHCUDP()
132+
go Start("../config/test.toml")
133+
//go Sub("../config/test.toml")
134+
/* s := Server{}
135+
s.Initialize()
136+
s.Run() */
137+
initRun = true
138+
time.Sleep(5000 * time.Millisecond)
139+
// Call nhcInit a second time to test updates
140+
NhcInit(&config.Conf.NhcConfig)
141+
142+
/* config.Conf.NhcConfig.Host = ConnectHost
143+
config.Conf.NhcConfig.Port, _ = strconv.Atoi(ConnectPort)
144+
config.Conf.ServerConfig.ListenPort = 8081
145+
config.Conf.ServerConfig.LogLevel = "DEBUG"
146+
*/
147+
//go NhcListener()
148+
//time.Sleep(1000 * time.Millisecond)
138149
//nhc.Init(&testConf)
139150
// call twice to test update items in persit.go
140151
//nhc.Init(&testConf)
141-
s := Server{}
142-
s.Initialize()
143-
go s.Run()
144-
go stubNHCUDP()
152+
145153
// not eleganr, but only for testing
146-
time.Sleep(5000 * time.Millisecond)
154+
//time.Sleep(5000 * time.Millisecond)
147155
//ws.Initialize()
148-
initRun = true
156+
149157
} else {
150158
retries++
151159
if retries > 120 {
@@ -479,8 +487,8 @@ func TestDiscover(t *testing.T) {
479487
name string
480488
want net.IP
481489
}{
482-
{"no nhc on LAN", nil},
483-
//{"stub nhc", getOutboundIP()},
490+
//{"no nhc on LAN", nil},
491+
{"stub nhc", getOutboundIP()},
484492
}
485493
portCheckIteration := 0
486494
for _, tt := range tests {
@@ -492,7 +500,17 @@ func TestDiscover(t *testing.T) {
492500
t.Run(tt.name, func(t *testing.T) {
493501
GotoTestPort:
494502
if IsTCPPortAvailable(18043) {
495-
if got := Discover(); !reflect.DeepEqual(got, tt.want) {
503+
testok := false
504+
got := Discover()
505+
if reflect.DeepEqual(got, tt.want) {
506+
testok = true
507+
}
508+
if !testok {
509+
if reflect.DeepEqual(got.String(), "10.32.2.50") {
510+
testok = true
511+
}
512+
}
513+
if !testok {
496514
t.Errorf("Discover() = %v, want %v", got, tt.want)
497515
}
498516
} else {

0 commit comments

Comments
 (0)