예제 #1
0
func Test_Agent_Recorder_Cancel(t *testing.T) {
	var agent *Agent
	var r Recorder
	assert := wcg.NewAssert(t)
	now, _ := wcg.ParseDateTime("2016-05-15T16:05:00Z")

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/agent_tests.json"))
		agent = NewAgent(mock.BaseURL(), "test-token", nil)
		agent.recorderFactory = func(iepg *pt.IEPG) Recorder {
			return NewMockRecorder(iepg)
		}
		lib.TemporarySetNow(now, func() {
			agent.RunOnce()
			time.Sleep(100 * time.Millisecond)
			r = agent.recorders["200171201605160105"]
			assert.EqInt(1, len(agent.recorders), "agent should have a recorder thread.")
			assert.EqInt(int(pt.IEPGProcessing), int(r.GetStats().Status), "the recorder status should be pt.IEPGProcessing.")
		})
	})

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		// preparation for dummy entry
		mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/agent_tests_no_data.json"))
		agent.subscriber = NewSubscriber(mock.BaseURL(), "test-token")
		lib.TemporarySetNow(now, func() {
			agent.RunOnce()
			time.Sleep(100 * time.Millisecond)
			assert.EqInt(0, len(agent.recorders), "agent should have no recorder thread.")
			assert.EqInt(int(r.GetStats().Status), int(pt.IEPGCanceled), "the recorder status should be pt.IEPGPRocessing.")
		})
	})
}
예제 #2
0
func Test_API_Task_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Task_List.json", nil), "fixture")

	now, _ := wcg.ParseDateTime("2016-01-10T00:00:00Z")

	lib.TemporarySetNow(now, func() {
		req := ts.GET("/api/admin/tasks/")
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got entities.Pagination
		assert.JSONResponse(&got, res)
		assert.EqInt(0, got.Length())
	})

	now, _ = wcg.ParseDateTime("2016-01-09T00:00:00Z")

	lib.TemporarySetNow(now, func() {
		req := ts.GET("/api/admin/tasks/")
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got entities.Pagination
		assert.JSONResponse(&got, res)
		assert.EqInt(1, got.Length())
	})
}
예제 #3
0
func Test_Kind_CreateEntityFromForm(t *testing.T) {
	assert := gaetest.NewAssert(t)

	n := lib.Now()
	lib.TemporarySetNow(n, func() {
		var ent, err = testEntity.CreateEntityFromForm(url.Values{
			"content_bytes": []string{"abcdef"},
			"slice_type":    []string{"1", "2", "3"},
		})
		assert.Nil(err)
		assert.EqStr("This is defualt value", ent.(*TEntity).Desc)
		assert.EqInt(10, ent.(*TEntity).Digit)
		assert.EqStr("abcdef", string(ent.(*TEntity).ContentBytes))
		assert.EqInt(3, len(ent.(*TEntity).SliceType))
		assert.EqStr("1", ent.(*TEntity).SliceType[0])
		assert.EqStr("2", ent.(*TEntity).SliceType[1])
		assert.EqStr("3", ent.(*TEntity).SliceType[2])
		assert.EqTime(n, ent.(*TEntity).CreatedAt)
	})

	var ent, err = testEntity.CreateEntityFromForm(url.Values{
		"digit": []string{"2"},
		"desc":  []string{"specific desc"},
	})
	assert.Nil(err)
	assert.EqInt(2, ent.(*TEntity).Digit)
	assert.EqStr("specific desc", ent.(*TEntity).Desc)
}
예제 #4
0
func Test_API_CronStatsCleanup(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_SystemStats_Query.json", nil, "intern.home"), "fixture")

	sk, _ := Server.Get().Key("dummy").MustOne(ts.GET("/").Request)
	c := SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request)
	assert.EqInt(2, c)

	tempnow, _ := wcg.ParseDateTime("2014-12-01T11:00:00Z")
	lib.TemporarySetNow(tempnow, func() {
		app := NewApp()
		req := ts.GET("/cron/intern/home/stats/cleanup/")
		res := req.RouteTo(app.Routes())
		assert.HTTPStatus(200, res)

		// confirm nothing deleted
		c = SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request)
		assert.EqInt(2, c)
	})

	app := NewApp()
	req := ts.GET("/cron/intern/home/stats/cleanup/")
	res := req.RouteTo(app.Routes())
	assert.HTTPStatus(200, res)

	// everything deleted since it's old
	c = SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request)
	assert.EqInt(0, c)
}
예제 #5
0
func Test_API_SystemStats_Query(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_SystemStats_Query.json", nil, "intern.home"), "fixture")

	app := NewApp()
	req := ts.GET("/api/intern/home/stats/servers/dummy/system/")
	res := req.RouteTo(app.Routes())
	assert.HTTPStatus(200, res)
	var got []home.SystemStats
	assert.JSONResponse(&got, res)
	assert.EqInt(0, len(got), string(res.Body))

	req = ts.GET("/api/intern/home/stats/servers/dummy/system/?until=2014-12-01T11:00:00Z")
	res = req.RouteTo(app.Routes())
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqInt(2, len(got), string(res.Body))

	tempnow, _ := wcg.ParseDateTime("2014-12-01T11:00:00Z")
	lib.TemporarySetNow(tempnow, func() {
		req := ts.GET("/api/intern/home/stats/servers/dummy/system/")
		res := req.RouteTo(app.Routes())
		assert.HTTPStatus(200, res)
		var got []home.SystemStats
		assert.JSONResponse(&got, res)
		assert.EqInt(2, len(got), string(res.Body))
	})

}
예제 #6
0
func Test_API_IEPGRecord_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGRecord_List.json", nil, "intern.pt"), "fixture")

	lib.TemporarySetNow(now, func() {
		req := ts.GET("/api/intern/pt/iepg/records/")
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got entities.Pagination
		assert.JSONResponse(&got, res)
		assert.EqInt(1, got.Length(), "%v", got)
	})
}
예제 #7
0
func Test_Subscribe(t *testing.T) {
	assert := wcg.NewAssert(t)

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		// preparation for dummy entry
		mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/subscriber_tests.json"))
		s := NewSubscriber(mock.BaseURL(), "test-token")

		t, _ := wcg.ParseDateTime("2016-05-12T16:00:00Z")
		lib.TemporarySetNow(t, func() {
			list, err := s.Subscribe()
			assert.Nil(err, "error should be nil")
			assert.EqInt(1, len(list), "Subscribe should return the list of *pt.IEPG")
		})

		t, _ = wcg.ParseDateTime("2016-05-13T18:00:00Z")
		lib.TemporarySetNow(t, func() {
			list, err := s.Subscribe()
			assert.Nil(err, "error should be nil")
			assert.EqInt(0, len(list), "Subscribe should not return any IEPG")
		})
	})
}
예제 #8
0
func Test_API_IEPGExclusion_RunTask(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGExclusion_RunTask.json", nil, "intern.pt"))

	now, _ := wcg.ParseDateTime("2016-01-01T00:00:00Z")
	lib.TemporarySetNow(now, func() {
		runner := helper.NewAsyncTaskTestRunner(t, instance.Routes(), ts)
		runner.OnTrigger(func(req *httptest.TestRequest, trigger func()) {
			helper.TemporaryAllow(req, "family", trigger)
		})
		runner.OnMonitor(func(req *httptest.TestRequest, monitor func()) {
			helper.TemporaryAllow(req, "family", monitor)
		})
		runner.Run("/api/intern/pt/iepg/exclusions/task/", url.Values{})

		req := ts.GET("/dummy")
		_, flagged := IEPG.Get().Key("test1").MustOne(req.Request)
		assert.OK(len(flagged.(*pt.IEPG).ExcludedBy) > 0)

	})
}
예제 #9
0
func Test_Agent_Recorder_Completed(t *testing.T) {
	assert := wcg.NewAssert(t)

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		// preparation for dummy entry
		mock.Routes().GET("/api/intern/pt/iepg/records/", middleware.ServeFile("./fixtures/mocks/agent_tests.json"))
		agent := NewAgent(mock.BaseURL(), "test-token", nil)
		agent.recorderFactory = func(iepg *pt.IEPG) Recorder {
			return NewMockRecorder(iepg)
		}
		now, _ := wcg.ParseDateTime("2016-05-15T16:05:00Z")
		lib.TemporarySetNow(now, func() {
			agent.RunOnce() // started
			r := agent.recorders["200171201605160105"]

			agent.RunOnce() // notify update
			agent.RunOnce() // notify update
			agent.RunOnce() // notify update
			agent.RunOnce() // notify update and confirmed to be completed.
			assert.EqInt(0, len(agent.recorders), "agent should have no recorder thread.")
			assert.EqInt(int(r.GetStats().Status), int(pt.IEPGCompleted), "the recorder status should be pt.IEPGCompleted.")
		})
	})
}