From dbe87bcca544b86c868a350eeb96e3c38c0dc7dd Mon Sep 17 00:00:00 2001 From: postmannen Date: Mon, 23 May 2022 05:51:06 +0200 Subject: [PATCH] added table tests for reqeusts --- requests_test.go | 15 ++++++------ steward_test.go | 62 +++++++++++------------------------------------- 2 files changed, 22 insertions(+), 55 deletions(-) diff --git a/requests_test.go b/requests_test.go index e5a6422..a76ea59 100644 --- a/requests_test.go +++ b/requests_test.go @@ -9,7 +9,7 @@ import ( natsserver "github.com/nats-io/nats-server/v2/server" ) -func newServerForTesting(t *testing.T) *server { +func newServerForTesting(t *testing.T, addressAndPort string) (*server, *Configuration) { //if !*logging { // log.SetOutput(io.Discard) //} @@ -21,7 +21,7 @@ func newServerForTesting(t *testing.T) *server { // Create the config to run a steward instance. //tempdir := "./tmp" conf := newConfigurationDefaults() - conf.BrokerAddress = "127.0.0.1:42222" + conf.BrokerAddress = addressAndPort conf.NodeName = "central" conf.CentralNodeName = "central" conf.ConfigFolder = "tmp" @@ -29,21 +29,22 @@ func newServerForTesting(t *testing.T) *server { conf.SocketFolder = "tmp" conf.SubscribersDataFolder = "tmp" conf.DatabaseFolder = "tmp" + conf.StartSubREQErrorLog = true stewardServer, err := NewServer(&conf, "test") if err != nil { t.Fatalf(" * failed: could not start the Steward instance %v\n", err) } - return stewardServer + return stewardServer, &conf } // Start up the nats-server message broker for testing purposes. -func newNatsServerForTesting(t *testing.T) *natsserver.Server { +func newNatsServerForTesting(t *testing.T, port int) *natsserver.Server { // Start up the nats-server message broker. nsOpt := &natsserver.Options{ Host: "127.0.0.1", - Port: 42222, + Port: port, } ns, err := natsserver.NewServer(nsOpt) @@ -66,13 +67,13 @@ func TestRequest(t *testing.T) { containsOrEquals } - ns := newNatsServerForTesting(t) + ns := newNatsServerForTesting(t, 42222) if err := natsserver.Run(ns); err != nil { natsserver.PrintAndDie(err.Error()) } defer ns.Shutdown() - srv := newServerForTesting(t) + srv, _ := newServerForTesting(t, "127.0.0.1:42222") srv.Start() defer srv.Stop() diff --git a/steward_test.go b/steward_test.go index d1b1810..e14e367 100644 --- a/steward_test.go +++ b/steward_test.go @@ -29,50 +29,19 @@ var logging = flag.Bool("logging", false, "set to true to enable the normal logg // Message passing. // The different REQ types. func TestStewardServer(t *testing.T) { - if !*logging { - log.SetOutput(io.Discard) + // if !*logging { + // log.SetOutput(io.Discard) + // } + + ns := newNatsServerForTesting(t, 40222) + if err := natsserver.Run(ns); err != nil { + natsserver.PrintAndDie(err.Error()) } + defer ns.Shutdown() - // Start the nats-server message broker. - startNatsServerTest(t) - - // Start Steward instance - // --------------------------------------- - // tempdir := t.TempDir() - - // Create the config to run a steward instance. - tempdir := "./tmp" - conf := &Configuration{ - SocketFolder: filepath.Join(tempdir, "tmp"), - DatabaseFolder: filepath.Join(tempdir, "var/lib"), - //SubscribersDataFolder: filepath.Join(tempdir, "data"), - SubscribersDataFolder: "./tmp/", - ConfigFolder: "./tmp/etc", - BrokerAddress: "127.0.0.1:40222", - PromHostAndPort: ":2112", - NodeName: "central", - CentralNodeName: "central", - DefaultMessageRetries: 1, - DefaultMessageTimeout: 3, - EnableSocket: true, - // AllowEmptySignature: true, - EnableDebug: true, - - StartSubREQCliCommand: true, - StartSubREQCliCommandCont: true, - StartSubREQToConsole: true, - StartSubREQToFileAppend: true, - StartSubREQToFile: true, - StartSubREQHello: true, - StartSubREQErrorLog: true, - StartSubREQTailFile: true, - // StartSubREQToSocket: flagNodeSlice{OK: true, Values: []Node{"*"}}, - } - stewardServer, err := NewServer(conf, "test") - if err != nil { - t.Fatalf(" * failed: could not start the Steward instance %v\n", err) - } - stewardServer.Start() + srv, conf := newServerForTesting(t, "127.0.0.1:40222") + srv.Start() + defer srv.Stop() // Run the message tests // @@ -95,14 +64,11 @@ func TestStewardServer(t *testing.T) { } for _, f := range funcs { - err := f(stewardServer, conf, t) + err := f(srv, conf, t) if err != nil { t.Errorf("%v\n", err) } } - // --------------------------------------- - - stewardServer.Stop() } @@ -213,7 +179,7 @@ func checkREQErrorLogTest(stewardServer *server, conf *Configuration, t *testing // Check the tailing of files type. func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing.T) error { // Create a file with some content. - fh, err := os.OpenFile("test.file", os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600) + fh, err := os.OpenFile("tmp/test.file", os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0600) if err != nil { return fmt.Errorf(" * failed: unable to open temporary file: %v", err) } @@ -248,7 +214,7 @@ func checkREQTailFileTest(stewardServer *server, conf *Configuration, t *testing return fmt.Errorf(" \U0001F631 [FAILED] : checkREQTailFileTest: : getting current working directory: %v", err) } - file := filepath.Join(wd, "test.file") + file := filepath.Join(wd, "tmp/test.file") s := `[ {