示例#1
0
文件: giles.go 项目: gtfierro/giles2
func main() {

	signals := make(chan os.Signal, 1)
	done := make(chan bool)
	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		sig := <-signals
		log.Noticef("Got signal %v", sig)
		done <- true
	}()

	go func() {
		for {
			time.Sleep(5 * time.Second)
			log.Infof("Number of active goroutines %v", runtime.NumGoroutine())
		}
	}()

	//time.AfterFunc(30*time.Second, func() {
	//	panic("STOP")
	//})

	flag.Parse()
	config := archiver.LoadConfig(*configfile)
	archiver.PrintConfig(config)

	/** Configure CPU profiling */
	if config.Profile.Enabled {
		log.Infof("Benchmarking for %v seconds\n", *config.Profile.BenchmarkTimer)
		f, err := os.Create(*config.Profile.CpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		f2, err := os.Create("blockprofile.db")
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		runtime.SetBlockProfileRate(1)

		f3, err := os.Create("trace.out")
		if err != nil {
			log.Fatal(err)
		}
		trace.Start(f3)
		defer runtime.SetBlockProfileRate(0)
		defer pprof.Lookup("block").WriteTo(f2, 1)
		defer pprof.StopCPUProfile()
	}

	a := archiver.NewArchiver(config)

	if config.HTTP.Enabled {
		go http.Handle(a, *config.HTTP.Port)
	}

	if config.BOSSWAVE.Enabled {
		go bosswave.Handle(a, &config.BOSSWAVE)
	}

	if config.WebSocket.Enabled {
		go websocket.Handle(a, *config.WebSocket.Port)
	}

	if config.MsgPackUDP.Enabled {
		go msgpack.HandleUDP4(a, *config.MsgPackUDP.Port)
	}

	if config.TCPJSON.Enabled {
		go tcpjson.Handle(a, *config.TCPJSON.AddPort, *config.TCPJSON.QueryPort, *config.TCPJSON.SubscribePort)
	}

	<-done
}
示例#2
0
func TestHandleAdd(t *testing.T) {
	aConfig := giles.LoadConfig("../giles.cfg")
	testArchiver = giles.NewArchiver(aConfig)
	h := NewHTTPHandler(testArchiver)
	uuid := common.NewUUID()

	for _, test := range []struct {
		title          string
		toPost         string
		expectedStatus int
		expectedBody   string
	}{
		{
			"No Readings",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v" }}`, uuid),
			200,
			"",
		},
		{
			"Empty Readings 1",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [ ]}}`, uuid),
			200,
			"",
		},
		{
			"Empty Readings 2",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[ ]]}}`, uuid),
			200,
			"",
		},
		{
			"Bad JSON 1",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [ ]]}}`, uuid),
			400,
			"invalid character ']' after object key:value pair",
		},
		{
			"Bad JSON 2",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[ ]]}`, uuid),
			400,
			"unexpected EOF",
		},
		{
			"Bad Readings 1: negative timestamp",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[-1, 0]]}}`, uuid),
			400,
			"json: cannot unmarshal number -1 into Go value of type uint64",
		},
		{
			"Bad Readings 2: too big timestamp",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[1000000000000000000, 0]]}}`, uuid),
			500,
			"Bad Timestamp: 1000000000000000000",
		},
		{
			"Bad Readings 3: too big timestamp",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[3458764513820540929, 0]]}}`, uuid),
			500,
			"Bad Timestamp: 3458764513820540929",
		},
		{
			"Good readings: 1 reading max timestamp",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [[3458764513820540928, 0]]}}`, uuid),
			200,
			"",
		},
		{
			"Good readings: 2 repeat readings",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [[1000000000000000000, 0], [1000000000000000000, 0]]}}`, uuid),
			200,
			"",
		},
		{
			"Good readings: 2 repeat readings diff values",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [[2000000000000000000, 0], [2000000000000000000, 1]]}}`, uuid),
			200,
			"",
		},
		{
			"Lots of readings",
			fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [ %v [1000000000000000000, 0]]}}`, uuid, strings.Repeat("[1000000000000000000, 0],", 1000)),
			200,
			"",
		},
	} {

		testflight.WithServer(h.handler, func(r *testflight.Requester) {
			response := r.Post("/add/dummykey", testflight.JSON, test.toPost)
			assert.Equal(t, test.expectedStatus, response.StatusCode, test.title)
			assert.Equal(t, test.expectedBody, response.Body, test.title)
		})
	}
}