func TestCurrentScalarState(t *testing.T) { Convey("When commit current message to database", t, func() { ev, err := evstore.Dial(mongoURL, "test", "events") So(err, ShouldBeNil) So(ev, ShouldNotBeNil) err = ev.Listenner2().Subscribe2("", Handler) So(err, ShouldBeNil) // Loop: fmt.Println("All messages read") for i := 1; i < 10; i++ { timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) log.Println(timestamp) rand.Seed(time.Now().UTC().UnixNano()) boxID := strconv.Itoa(rand.Intn(100)) varID := strconv.Itoa(rand.Intn(200)) boxID = "1" if i > 4 { varID = "3" } else { varID = "4" } val := strconv.FormatFloat(rand.NormFloat64(), 'f', 2, 64) sendMsg := "{\"datestamp\":\"" + timestamp + "\", \"box_id\": " + boxID + ", \"var_id\": " + varID + ", \"value\": " + val + "}" err = ev.Committer().SubmitEvent("123", "scalar", sendMsg) So(err, ShouldBeNil) } ev.Close() }) }
func TestSubmitEvent(t *testing.T) { Convey("Submit simple event", t, func() { var ( results []interface{} ) props := property.Init() CleanupCollections() ev, err := evstore.Dial(props["mongodb.url"], dbName, "submitevents") So(err, ShouldBeNil) So(ev, ShouldNotBeNil) c := makeStubClient() ctx, cancel := context.WithCancel(context.Background()) defer cancel() fromWS, _, _ := c.GetChannels() go handleClientRequest(ctx, c, ev) m := wsock.MessageT{} m["sequenceid"] = "" m["tag"] = "test" m["event"] = map[string]interface{}{"event": "fake"} fromWS <- &m <-time.After(time.Millisecond * 100) session, err := mgo.Dial(props["mongodb.url"]) defer session.Close() So(err, ShouldBeNil) So(session, ShouldNotBeNil) iter := session.DB(dbName).C("submitevents").Find(nil).Iter() So(iter, ShouldNotBeNil) iter.All(&results) So(results, ShouldNotBeNil) for _, v := range results { So(v.(bson.M)["event"].(bson.M)["event"], ShouldEqual, "fake") } }) }
func TestGetDistanceValueIncorrectPointNumber(t *testing.T) { evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("GetDistanceValue") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } tStart := time.Now() tStop := tStart.Add(10 * time.Second) _, err = f.(func(time.Time, time.Time, int) (chan string, error))(tStart, tStop, -10) expectedError := "numberOfPoints should be positive integer" if err.Error() != expectedError { t.Fatal(err.Error() + " != " + expectedError) } }
func TestGetDistanceValueZerroInterval(t *testing.T) { evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("GetDistanceValue") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } tStart := time.Now() _, err = f.(func(time.Time, time.Time, int) (chan string, error))(tStart, tStart, 10) expectedError := "To must be greater than From" if err.Error() != expectedError { t.Fatal(err.Error() + " != " + expectedError) } }
func TestListDatabases(t *testing.T) { evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("ListDatabases") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } dbList, err := f.(func() ([]string, error))() if err != nil { t.Fatal(err) } if len(dbList) == 0 { t.Fatal("List should not be empty") } }
func TestEventSrv(t *testing.T) { Convey("When commit current message to database", t, func() { ev, err := evstore.Dial(mongoURL, dbName, "events") So(err, ShouldBeNil) So(ev, ShouldNotBeNil) err = ev.Listenner2().Subscribe2("", handlerTest) So(err, ShouldBeNil) ctx := context.WithValue(context.Background(), "test", t) ctx, cancel := context.WithCancel(ctx) defer cancel() go ev.Listenner2().Listen(ctx, "") <-time.After(time.Second * 2) fmt.Println("All messages read") for i := 0; i < 10; i++ { timestamp := strconv.FormatInt(time.Now().Unix(), 10) log.Println(timestamp) rand.Seed(time.Now().UTC().UnixNano()) boxID := strconv.Itoa(rand.Intn(100)) varID := strconv.Itoa(rand.Intn(200)) val := strconv.FormatFloat(rand.NormFloat64(), 'f', 2, 64) sendMsg := "{\"datestamp\":\"" + timestamp + "\", \"box_id\": " + boxID + ", \"var_id\": " + varID + ", \"value\": " + val + "}" err = ev.Committer().SubmitEvent("123", "scalar", sendMsg) So(err, ShouldBeNil) } <-time.After(time.Second * 3) ev.Close() }) }
func main() { f, err := os.Create("currentsrv.prof") if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { select { case <-c: log.Println("Stop profiling") pprof.StopCPUProfile() syscall.Exit(0) } }() props := property.Init() evStore, err := evstore.Dial(props["mongodb.url"], props["mongodb.db"], props["mongodb.stream"]) if err != nil { log.Fatalln("Error connecting to event store. ", err) } wsServer := wsock.NewServer(props["events.uri"]) if wsServer == nil { log.Fatalln("Error creating new websocket server") } go processClientConnection(wsServer, evStore) go wsServer.Listen() //http.Handle(props["static.url"], http.FileServer(http.Dir("webroot"))) err = http.ListenAndServe(props["events.url"], nil) evStore.Close() }
func main() { var id string flag.StringVar(&id, "id", "", "ID to subscribe from") flag.Parse() props := property.Init() evStore, err := evstore.Dial(props["mongodb.url"], props["mongodb.db"], props["mongodb.stream"]) if err != nil { log.Fatalln("Error connecting to event store. ", err) } wsServer := wsock.NewServer(props["configsrv.uri"]) if wsServer == nil { log.Fatalln("Error creating new websocket server") } currentConfig = Config{} currentConfig.mx = &sync.Mutex{} isCurrent = false stateUpdateChannel := make(chan *bson.M, 256) err = evStore.Listenner2().Subscribe2("config", configHandler) if err != nil { log.Fatalln("Error subscribing for config changes", err) } ctx1, cancel := context.WithCancel(context.Background()) ctx := context.WithValue(ctx1, "stateUpdateChannel", stateUpdateChannel) defer cancel() log.Println("Before Listen call") go evStore.Listenner2().Listen(ctx, id) go processClientConnection(ctx, wsServer) go wsServer.Listen() //http.Handle(props["static.url"], http.FileServer(http.Dir("webroot"))) err = http.ListenAndServe(props["configsrv.url"], nil) evStore.Close() }
func TestGetHistory(t *testing.T) { var ( m map[string]interface{} ) evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("GetHistory") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } evStore.Manager().DropDatabase(dbName) msg1 := "{\"message\":\"NOT expected\"}" evStore.Committer().SubmitEvent("", "test", msg1) <-time.After(2 * time.Second) tStart := time.Now() for n := 0; n < 10; n++ { fmt.Printf(".") msg2 := "{\"message\":\"" + strconv.Itoa(n) + "\"}" evStore.Committer().SubmitEvent("", "test", msg2) <-time.After(500 * time.Millisecond) } tStop := time.Now() <-time.After(2 * time.Second) evStore.Committer().SubmitEvent("", "test", msg1) log.Println(tStart, " < ", tStop) c, err := f.(func(time.Time, time.Time) (chan string, error))(tStart, tStop) if err != nil { t.Fatal(err) } if c == nil { t.Fatal("Channel shouldn't be nil") } for { msg, ok := <-c if !ok { break } log.Println(msg) err = json.Unmarshal([]byte(msg), &m) if err != nil { t.Fatal(err) } // if m["event"].(map[string]interface{})["message"] != "expected" { // t.Fatal(m["event"].(map[string]interface{})["message"]) // } } }
func TestGetFirstEventByType(t *testing.T) { var ( m map[string]interface{} ) evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("GetFirstEvent") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } evStore.Manager().DropDatabase(dbName) for n := 0; n < 100; n++ { msg2 := "{\"Fake event before\":" + strconv.Itoa(n) + "}" evStore.Committer().SubmitEvent("", "scalar", msg2) } msg1 := "{\"message\":\"First event of type test\"}" evStore.Committer().SubmitEvent("", "test", msg1) for n := 0; n < 100; n++ { msg2 := "{\"Fake event\":" + strconv.Itoa(n) + "}" evStore.Committer().SubmitEvent("", "scalar", msg2) } c, err := f.(func(tag string) (chan string, error))("test") if err != nil { t.Fatal(err) } if c == nil { t.Fatal("Channel shouldn't be nil") } msgCounter := 0 for msg := range c { msgCounter = msgCounter + 1 err = json.Unmarshal([]byte(msg), &m) if err != nil { t.Fatal(err) } if m["event"].(map[string]interface{})["message"] != "First event of type test" { t.Fatal("Incorrect message retuned: ", m["message"]) } } if msgCounter == 0 { t.Fatal("No message recieved from database") } if msgCounter > 1 { t.Fatal("Too many messages returned", msgCounter) } }
func TestNewRPCFunctionInterface(t *testing.T) { c, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal("Error connecting to evstore") } rpc := NewRPCFunctionInterface(c) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } }
func main() { var ( fileName string uri string dbName string collectionName string msg string genFile string ) flag.StringVar(&fileName, "f", "", "JSON file name with events") flag.StringVar(&uri, "s", "", "Event store URI") flag.StringVar(&dbName, "db", "", "Database name") flag.StringVar(&collectionName, "c", "", "Collection name") flag.StringVar(&msg, "msg", "", "Message to send to database") flag.StringVar(&genFile, "gen", "", "Genfile with information") flag.Parse() evStore, err := evstore.Dial(uri, dbName, collectionName) if err != nil { log.Panicln(err) } if fileName != "" { if _, err = os.Stat(fileName); err != nil { log.Panicln(err) } f, err := os.Open(fileName) if err != nil { log.Panicln(err) } r := bufio.NewReader(f) send2EventStore(r, evStore) } if msg != "" { err = evStore.Committer().SubmitEvent("", "scalar", msg) if err != nil { log.Panicln(err) } } if genFile != "" { if _, err := os.Stat(genFile); err != nil { log.Panicln(err) } f, err := os.Open(genFile) if err != nil { log.Panicln(err) } r := bufio.NewReader(f) log.Println("Start GenDataFromGenFile") genDataFromGenFile(context.Background(), r, evStore) } evStore.Close() }
func submitEvent2Datastore(url string, database string, streamName string, seqid string, tag string, event map[string]interface{}) error { ev, err := evstore.Dial(url, database, streamName) if err != nil { return err } defer ev.Close() err = ev.Committer().SubmitMapStringEvent(seqid, tag, event) if err != nil { return err } return nil }
func main() { props := property.Init() evStore, err := evstore.Dial(props["mongodb.url"], props["mongodb.db"], props["mongodb.stream"]) if err != nil { log.Fatalln("Error connecting to event store. ", err) } f := RPCFunction{evStore} wsServer := wsock.NewServer(props["rpceventsrv.uri"]) if wsServer == nil { log.Fatalln("Error creating new websocket server") } go processClientConnection(wsServer, &f) go wsServer.Listen() //http.Handle(props["static.url"], http.FileServer(http.Dir("webroot"))) err = http.ListenAndServe(props["rpceventsrv.url"], nil) evStore.Close() }
func TestNegativeGetFunction(t *testing.T) { evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("EchoFake") if err == nil { t.Fatal(err) } if f != nil { t.Fatal("Function isn't nil") } }
func processClientConnection(s *wsock.Server, props property.PropSet) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() log.Println("Enter processClientConnection") addCh, delCh, doneCh, _ := s.GetChannels() log.Println("Get server channels", addCh, delCh, doneCh) Loop: for { select { case <-doneCh: log.Println("doneCh got message") break Loop case <-ctx.Done(): log.Println("Context destroyed") break Loop case cli := <-addCh: log.Println("processClientConnection got add client notification", cli.Request().FormValue("id")) streamName := cli.Conn().Request().FormValue("stream") if streamName == "" { streamName = props["mongodb.stream"] } ctx, cancel := context.WithCancel(context.Background()) defer cancel() log.Println("Stream name", streamName) evStore, err := evstore.Dial(props["mongodb.url"], props["mongodb.db"], streamName) defer evStore.Close() if err != nil { log.Fatalln("Error connecting to event store. ", err) return } go handleClientRequest(ctx, cli, evStore) break case cli := <-delCh: log.Println("delCh go client", cli) break } } log.Println("processClientConnection exited") }
func main() { var id string flag.StringVar(&id, "id", "", "ID to subscribe from") flag.Parse() props := property.Init() evStore, err := evstore.Dial(props["mongodb.url"], props["mongodb.db"], props["mongodb.stream"]) if err != nil { log.Fatalln("Error connecting to event store. ", err) } wsServer := wsock.NewServer(props["current.uri"]) if wsServer == nil { log.Fatalln("Error creating new websocket server") } sState = ScalarState{} sState.state = make(map[int]map[int]*bson.M) sState.mx = &sync.Mutex{} isCurrent = false stateUpdate := pubsub.NewPublisher(time.Millisecond*100, 1024) err = evStore.Listenner2().Subscribe2("scalar", scalarHandler) if err != nil { log.Fatalln("Error subscribing for changes", err) } ctx1, cancel := context.WithCancel(context.Background()) ctx := context.WithValue(ctx1, "stateUpdate", stateUpdate) defer cancel() sState.lastID = evStore.Listenner2().GetLastID() log.Println("Before Listen call") go evStore.Listenner2().Listen(ctx, id) go processClientConnection(ctx, wsServer) go wsServer.Listen() //http.Handle(props["static.url"], http.FileServer(http.Dir("webroot"))) err = http.ListenAndServe(props["current.url"], nil) evStore.Close() }
func TestGetEventAt(t *testing.T) { var ( m map[string]interface{} ) evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("GetEventAt") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } evStore.Manager().DropDatabase(dbName) msg1 := "{\"message\":\"First event\"}" evStore.Committer().SubmitEvent("", "test", msg1) for n := 0; n < 50; n++ { msg2 := "{\"Counter\":" + strconv.Itoa(n) + "}" evStore.Committer().SubmitEvent("", "test", msg2) } tPoint := time.Now() <-time.After(100 * time.Millisecond) for n := 50; n < 100; n++ { msg2 := "{\"Counter\":" + strconv.Itoa(n) + "}" evStore.Committer().SubmitEvent("", "test", msg2) } msgLast := "{\"message\":\"Last event\"}" evStore.Committer().SubmitEvent("", "test", msgLast) c, err := f.(func(tag string, timePoint time.Time) (chan string, error))("", tPoint) if err != nil { t.Fatal(err) } if c == nil { t.Fatal("Channel shouldn't be nil") } msgCounter := 0 for msg := range c { log.Println(msg) msgCounter = msgCounter + 1 err = json.Unmarshal([]byte(msg), &m) if err != nil { t.Fatal(err) } if int(m["event"].(map[string]interface{})["Counter"].(float64)) != 49 { t.Fatal("Incorrect message retuned: ", m["event"]) } } if msgCounter == 0 { t.Fatal("No message recieved from database") } if msgCounter > 1 { t.Fatal("Too many messages returned", msgCounter) } }
func TestGetDistanceValue(t *testing.T) { var ( m map[string]interface{} ) evStore, err := evstore.Dial("localhost", dbName, "test") if err != nil { t.Fatal(err) } rpc := NewRPCFunctionInterface(evStore) if err != nil { t.Fatal(err) } if rpc == nil { t.Fatal("RPCFunctionInterface is nil") } f, err := rpc.GetFunction("GetDistanceValue") if err != nil { t.Fatal(err) } if f == nil { t.Fatal("Function is nil") } evStore.Manager().DropDatabase(dbName) msg1 := "{\"message\":\"NOT expected\"}" evStore.Committer().SubmitEvent("", "test", msg1) <-time.After(1 * time.Second) tStart := time.Now() <-time.After(10 * time.Millisecond) for n := 0; n < 100; n++ { fmt.Printf(".") msg2 := "{\"value\":" + strconv.Itoa(n) + "}" evStore.Committer().SubmitEvent("", "scalar", msg2) <-time.After(100 * time.Millisecond) } tStop := time.Now() <-time.After(2 * time.Second) evStore.Committer().SubmitEvent("", "test", msg1) log.Println(tStart, " < ", tStop) c, err := f.(func(time.Time, time.Time, int) (chan string, error))(tStart, tStop, 10) if err != nil { t.Fatal(err) } if c == nil { t.Fatal("Channel shouldn't be nil") } msgCounter := 0 for { msg, ok := <-c if !ok { break } msgCounter = msgCounter + 1 // log.Println(msg) err = json.Unmarshal([]byte(msg), &m) if err != nil { t.Fatal(err) } } if msgCounter == 0 { t.Fatal("No message recieved from database") } }
func TestSubmitErrorEvent(t *testing.T) { Convey("Submit event with no tag", t, func() { var ( results []interface{} ) props := property.Init() CleanupCollections() ev, err := evstore.Dial(props["mongodb.url"], dbName, "submitevents") So(err, ShouldBeNil) So(ev, ShouldNotBeNil) c := makeStubClient() ctx, cancel := context.WithCancel(context.Background()) defer cancel() fromWS, toWS, _ := c.GetChannels() go handleClientRequest(ctx, c, ev) m := wsock.MessageT{} m["sequenceid"] = "" m["event"] = map[string]interface{}{"event": "no tag"} fromWS <- &m <-time.After(time.Millisecond * 100) msg := <-toWS So((*msg)["reply"].(string), ShouldEqual, "ERROR") So((*msg)["msg"].(string), ShouldEqual, "No tag") session, err := mgo.Dial(props["mongodb.url"]) So(err, ShouldBeNil) So(session, ShouldNotBeNil) defer session.Close() iter := session.DB(dbName).C("submitevents").Find(nil).Iter() So(iter, ShouldNotBeNil) iter.All(&results) So(results, ShouldBeNil) }) Convey("Submit event with no event", t, func() { var ( results []interface{} ) props := property.Init() CleanupCollections() ev, err := evstore.Dial(props["mongodb.url"], dbName, "submitevents") So(err, ShouldBeNil) So(ev, ShouldNotBeNil) c := makeStubClient() ctx, cancel := context.WithCancel(context.Background()) defer cancel() fromWS, toWS, _ := c.GetChannels() go handleClientRequest(ctx, c, ev) m := wsock.MessageT{} m["sequenceid"] = "" m["tag"] = "test" fromWS <- &m <-time.After(time.Millisecond * 100) msg := <-toWS So((*msg)["reply"].(string), ShouldEqual, "ERROR") So((*msg)["msg"].(string), ShouldEqual, "No event") session, err := mgo.Dial(props["mongodb.url"]) So(err, ShouldBeNil) So(session, ShouldNotBeNil) defer session.Close() iter := session.DB(dbName).C("submitevents").Find(nil).Iter() So(iter, ShouldNotBeNil) iter.All(&results) So(results, ShouldBeNil) }) }