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 handlePostRequest(w http.ResponseWriter, req *http.Request) { props := property.Init() data, err := readRequestBody(req) if err != nil { io.WriteString(w, err.Error()) return } msg, err := parseData(data) if err != nil { io.WriteString(w, err.Error()) return } streamName := props["mongodb.stream"] if req.FormValue("stream") != "" { streamName = req.FormValue("stream") } seqid, tag, event, err := extractEvent(msg) if err != nil { io.WriteString(w, err.Error()) return } err = submitEvent2Datastore(props["mongodb.url"], props["mongodb.db"], streamName, seqid, tag, event) if err != nil { io.WriteString(w, err.Error()) return } io.WriteString(w, "{\"reply\":\"ok\"}") }
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 CleanupCollections() { props := property.Init() session, _ := mgo.Dial(props["mongodb.url"]) _ = session.DB(dbName).C("submitevents").DropCollection() _ = session.DB(dbName).C("submitevents_capped").DropCollection() }
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 getDatabaseNames() ([]string, error) { props := property.Init() session, err := mgo.Dial(props["mongodb.url"]) if err != nil { return nil, err } return session.DatabaseNames() }
func TestDropDatabase(t *testing.T) { Convey("Before start testing we drop database", t, func() { props := property.Init() session, err := mgo.Dial(props["mongodb.url"]) So(session, ShouldNotBeNil) So(err, ShouldBeNil) _ = session.DB("test").C("events_capped").DropCollection() _ = session.DB("test").C("events").DropCollection() }) }
func dropTestDatabase(dbName string) error { props := property.Init() session, err := mgo.Dial(props["mongodb.url"]) if err != nil { return err } _ = session.DB(dbName).C("events_capped").DropCollection() _ = session.DB(dbName).C("events").DropCollection() 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 main() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { select { case <-c: syscall.Exit(0) } }() props := property.Init() wsServer := wsock.NewServer(props["submitevents.uri"]) if wsServer == nil { log.Fatalln("Error creating new websocket server") } go processClientConnection(wsServer, props) go wsServer.Listen() http.HandleFunc(props["postevents.uri"], handlePostRequest) go http.ListenAndServe(props["submitevents.url"], nil) err := http.ListenAndServeTLS(props["securepostevents.url"], "server.pem", "server.key", nil) if err != nil { log.Fatalln("Error while ListenAndServer", err) } }
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 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) }) }