func TestKittychanInfoSource(t *testing.T) {
	f, err := os.Open("data/www.kittychan.info/information.html")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	source := NewKittychanInfoSource()
	feed, err := source.ScrapeFromReader(f)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := time.LoadLocation("Asia/Tokyo")
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, 100, len(feed.Items))
	assert.Equal(t, "多摩センターイルミネーション\u3000今年も開催~キティちゃんのイルミネーション&オープニングセレモニーでキティちゃんのショー開催&キティちゃん達のパレードも~", feed.Items[0].Title)
	assert.Equal(t, "http://www.tamacenter-cm.com/illumi/", feed.Items[0].Link.Href)
	assert.WithinDuration(t, time.Date(2016, 11, 6, 0, 0, 0, 0, loc), feed.Items[0].Created, 0)

	assert.Equal(t, "「Hello Kitty Japanダイバーシティ東京店」 リニューアルオープン", feed.Items[99].Title)
	assert.Equal(t, "http://www.sanrio.co.jp/news/kt-gu-hkj-odaiba-renewal-20160921/", feed.Items[99].Link.Href)
	assert.WithinDuration(t, time.Date(2016, 9, 21, 0, 0, 0, 0, loc), feed.Items[99].Created, 0)
}
Example #2
0
func TestStartJob(t *testing.T) {
	ts := NewTestServer()
	defer ts.Close()
	kc := New(ts.URL)
	j := NewJobMap()

	id, err := kc.CreateJob(j)
	assert.NoError(t, err)
	assert.NotEqual(t, id, "")

	now := time.Now()
	ok, err := kc.StartJob(id)
	assert.NoError(t, err)
	assert.True(t, ok)

	// Wait let the job run
	time.Sleep(time.Second * 1)

	respJob, err := kc.GetJob(id)
	assert.NoError(t, err)
	assert.Equal(t, uint(1), respJob.Metadata.SuccessCount)
	assert.WithinDuration(t, now, respJob.Metadata.LastSuccess, time.Second*2)
	assert.WithinDuration(t, now, respJob.Metadata.LastAttemptedRun, time.Second*2)

	cleanUp()
}
Example #3
0
func TestAfter(t *testing.T) {
	b := &SimpleBackoff{
		Factor:      2,
		Min:         100 * time.Millisecond,
		Max:         2 * time.Second,
		MaxAttempts: 1,
	}
	var t1, t2, t3 time.Time
	var err1, err2 error
	t1 = time.Now()
	select {
	case err1 = <-After(b):
		t2 = time.Now()
	case <-time.After(101 * time.Millisecond):
		t.Error("Not executed on time")
	}
	select {
	case err2 = <-After(b):
	case <-time.After(time.Millisecond):
		t.Error("Not executed on time")
	}
	t3 = time.Now()
	assert.WithinDuration(t, t1.Add(100*time.Millisecond), t2, time.Millisecond)
	assert.WithinDuration(t, t2, t3, time.Millisecond)
	assert.Nil(t, err1)
	assert.NotNil(t, err2)

}
Example #4
0
func TestKalaStats(t *testing.T) {
	cache := NewMockCache()

	for i := 0; i < 5; i++ {
		j := GetMockJobWithGenericSchedule()
		j.Init(cache)
		j.Run(cache)
	}
	now := time.Now()
	for i := 0; i < 5; i++ {
		j := GetMockJobWithGenericSchedule()
		j.Init(cache)
		j.Disable()
	}

	kalaStat := NewKalaStats(cache)
	createdAt := time.Now()
	nextRunAt := time.Now().Add(
		time.Duration(time.Minute * 5),
	)

	assert.Equal(t, kalaStat.Jobs, 10)
	assert.Equal(t, kalaStat.ActiveJobs, 5)
	assert.Equal(t, kalaStat.DisabledJobs, 5)
	assert.Equal(t, kalaStat.SuccessCount, uint(5))
	assert.Equal(t, kalaStat.ErrorCount, uint(0))
	assert.WithinDuration(t, kalaStat.NextRunAt, nextRunAt, time.Second)
	assert.WithinDuration(t, kalaStat.LastAttemptedRun, now, time.Millisecond*100)
	assert.WithinDuration(t, kalaStat.CreatedAt, createdAt, time.Millisecond*100)
}
Example #5
0
func TestStateUpdateCheckWait(t *testing.T) {
	cws := NewUpdateCheckWaitState()

	// no update
	var tstart, tend time.Time

	tstart = time.Now()
	s, c := cws.Handle(nil, &stateTestController{
		pollIntvl: 100 * time.Millisecond,
	})
	tend = time.Now()
	assert.IsType(t, &UpdateCheckState{}, s)
	assert.False(t, c)
	assert.WithinDuration(t, tend, tstart, 105*time.Millisecond)

	// asynchronously cancel state operation
	go func() {
		c := cws.Cancel()
		assert.True(t, c)
	}()
	// should finish right away
	tstart = time.Now()
	s, c = cws.Handle(nil, &stateTestController{
		pollIntvl: 100 * time.Millisecond,
	})
	tend = time.Now()
	// canceled state should return itself
	assert.IsType(t, &UpdateCheckWaitState{}, s)
	assert.True(t, c)
	assert.WithinDuration(t, tend, tstart, 5*time.Millisecond)
}
Example #6
0
func TestDependentJobs(t *testing.T) {
	db := GetBoltDB(testDbPath)
	cache := NewMemoryJobCache(db, time.Second*5)

	mockJob := GetMockJobWithGenericSchedule()
	mockJob.Name = "mock_parent_job"
	mockJob.Init(cache)

	mockChildJob := GetMockJob()
	mockChildJob.ParentJobs = []string{
		mockJob.Id,
	}
	mockChildJob.Init(cache)

	assert.Equal(t, mockJob.DependentJobs[0], mockChildJob.Id)
	assert.True(t, len(mockJob.DependentJobs) == 1)

	j, err := cache.Get(mockJob.Id)
	assert.NoError(t, err)

	assert.Equal(t, j.DependentJobs[0], mockChildJob.Id)

	j.Run(cache)
	time.Sleep(time.Second * 2)
	n := time.Now()

	assert.WithinDuration(t, mockChildJob.LastAttemptedRun, n, 4*time.Second)
	assert.WithinDuration(t, mockChildJob.LastSuccess, n, 4*time.Second)
	db.Close()
}
func TestSanrioNewsReleaseSource(t *testing.T) {
	f, err := os.Open("data/www.sanrio.co.jp/corporate/release/index.html")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	doc, err := goquery.NewDocumentFromReader(f)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := time.LoadLocation("Asia/Tokyo")
	if err != nil {
		t.Fatal(err)
	}

	source := NewSanrioNewsReleaseSource()
	feed, err := source.ScrapeFromDocument(doc)
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, 51, len(feed.Items))
	assert.Equal(t, "ぐでぐでやる気のない「ぐでたま」のイベント九州初上陸! 夏休み企画 「ぐでたま in ふくおか」 7月21日(木)〜 福岡パルコ & sanrio vivitix 天神地下街店にて開催 (PDF)", feed.Items[0].Title)
	assert.Equal(t, "http://www.sanrio.co.jp/wp-content/uploads/2015/05/20160708-1.pdf", feed.Items[0].Link.Href)
	assert.WithinDuration(t, time.Date(2016, 7, 8, 0, 0, 0, 0, loc), feed.Items[0].Created, 0)
	assert.Equal(t, "2016年バレンタイン向けスペシャルギフト「GODIVA &ハローキティ」・「GODIVA &マイメロディ」1月6日(水)よりサンリオ限定販売", feed.Items[50].Title)
	assert.Equal(t, "http://www.sanrio.co.jp/corporate/release/y2016/d0106/", feed.Items[50].Link.Href)
	assert.WithinDuration(t, time.Date(2016, 1, 6, 0, 0, 0, 0, loc), feed.Items[50].Created, 0)
}
Example #8
0
// TestSetDataSetsMeta tests that...yeah
func TestSetDataSetsMeta(t *testing.T) {
	sts := SimpleTestSetup{
		NamespaceName: "TestSetDataSetsMeta",
		WorkSpecName:  "spec",
	}
	sts.SetUp(t)
	defer sts.TearDown(t)

	meta, err := sts.WorkSpec.Meta(false)
	if assert.NoError(t, err) {
		assert.Equal(t, 0, meta.Priority)
		assert.Equal(t, 20, meta.Weight)
		assert.False(t, meta.Paused)
		assert.False(t, meta.Continuous)
		assert.False(t, meta.CanBeContinuous)
		assert.Zero(t, meta.Interval)
		assert.WithinDuration(t, time.Time{}, meta.NextContinuous, 1*time.Microsecond)
		assert.Equal(t, 0, meta.MaxRunning)
		assert.Equal(t, 0, meta.MaxAttemptsReturned)
		assert.Equal(t, "", meta.NextWorkSpecName)
		assert.Equal(t, 0, meta.AvailableCount)
		assert.Equal(t, 0, meta.PendingCount)
		assert.Equal(t, "", meta.Runtime)
	}

	err = sts.WorkSpec.SetData(map[string]interface{}{
		"name":        "spec",
		"min_gb":      1,
		"priority":    10,
		"weight":      100,
		"disabled":    true,
		"continuous":  true,
		"interval":    60,
		"max_running": 10,
		"max_getwork": 1,
		"then":        "spec2",
		"runtime":     "go",
	})
	assert.NoError(t, err)

	meta, err = sts.WorkSpec.Meta(false)
	if assert.NoError(t, err) {
		assert.Equal(t, 10, meta.Priority)
		assert.Equal(t, 100, meta.Weight)
		assert.True(t, meta.Paused)
		assert.True(t, meta.Continuous)
		assert.True(t, meta.CanBeContinuous)
		assert.Equal(t, 60*time.Second, meta.Interval)
		assert.WithinDuration(t, time.Time{}, meta.NextContinuous, 1*time.Microsecond)
		assert.Equal(t, 10, meta.MaxRunning)
		assert.Equal(t, 1, meta.MaxAttemptsReturned)
		assert.Equal(t, "spec2", meta.NextWorkSpecName)
		assert.Equal(t, 0, meta.AvailableCount)
		assert.Equal(t, 0, meta.PendingCount)
		assert.Equal(t, "go", meta.Runtime)
	}
}
Example #9
0
func TestStateCancellable(t *testing.T) {
	cs := NewCancellableState(BaseState{
		id: MenderStateAuthorizeWait,
	})

	assert.Equal(t, MenderStateAuthorizeWait, cs.Id())

	var s State
	var c bool

	// no update
	var tstart, tend time.Time

	tstart = time.Now()
	s, c = cs.StateAfterWait(bootstrappedState, initState,
		100*time.Millisecond)
	tend = time.Now()
	// not cancelled should return the 'next' state
	assert.Equal(t, bootstrappedState, s)
	assert.False(t, c)
	assert.WithinDuration(t, tend, tstart, 105*time.Millisecond)

	// asynchronously cancel state operation
	go func() {
		c := cs.Cancel()
		assert.True(t, c)
	}()
	// should finish right away
	tstart = time.Now()
	s, c = cs.StateAfterWait(bootstrappedState, initState,
		100*time.Millisecond)
	tend = time.Now()
	// canceled should return the other state
	assert.Equal(t, initState, s)
	assert.True(t, c)
	assert.WithinDuration(t, tend, tstart, 5*time.Millisecond)

	// same thing again, but calling Wait() now
	go func() {
		c := cs.Cancel()
		assert.True(t, c)
	}()
	// should finish right away
	tstart = time.Now()
	wc := cs.Wait(100 * time.Millisecond)
	tend = time.Now()
	assert.False(t, wc)
	assert.WithinDuration(t, tend, tstart, 5*time.Millisecond)

	// let wait finish
	tstart = time.Now()
	wc = cs.Wait(100 * time.Millisecond)
	tend = time.Now()
	assert.True(t, wc)
	assert.WithinDuration(t, tend, tstart, 105*time.Millisecond)
}
Example #10
0
func TestIntervalSMS(t *testing.T) {
	now := Now()

	tests := []struct {
		start, finish time.Time
		msgs          []string
	}{
		{now, now.Add(3 * time.Minute), []string{
			"Message 0", "Message 1", "Message 2",
		}},
	}

	for _, test := range tests {
		rems, err := IntervalSMS("0123456789", test.msgs, test.start, test.finish)
		if err != nil {
			t.Errorf("Error from IntervalSMS: %v", err)
			continue
		}

		// Every reminder should be scheduled within window (between
		// start and finish)

		for i, rem := range rems {
			t.Logf("rem[%d] == %#v\n", i, rem)

			assert.WithinDuration(t,
				test.start,
				rem.NextRun,
				test.finish.Sub(test.start),
				fmt.Sprintf(
					"Reminder scheduled for %s, should be between %s and %s",
					rem.NextRun, test.start, test.finish))
		}

		oneChunk := test.finish.Sub(test.start) / time.Duration(len(test.msgs))

		t.Logf("oneChunk == %v", oneChunk)

		// Each pair of messages should be at most oneChunk*2 apart

		for i := 0; i < len(rems)-1; i++ {
			// Adjacent reminders _should_ be closer than oneChunk * 2
			assert.WithinDuration(
				t,
				rems[i].NextRun,
				rems[i+1].NextRun,
				oneChunk*time.Duration(2),
				fmt.Sprintf("Reminders %v and %v scheduled too far apart (%v)",
					rems[i],
					rems[i+1],
					time.Duration(2)*rems[i+1].NextRun.Sub(rems[i].NextRun)),
			)
		}
	}

}
Example #11
0
func (suite *UserStoreSuite) TestGetValidUser() {
	u, err := suite.store.Get(suite.user.Email)

	assert.Nil(suite.T(), err)

	assert.Equal(suite.T(), suite.user.Email, u.Email)
	assert.Equal(suite.T(), suite.user.Password, u.Password)
	assert.Equal(suite.T(), suite.user.Salt, u.Salt)
	assert.WithinDuration(suite.T(), suite.user.Updated, u.Updated, 1*time.Second)
	assert.WithinDuration(suite.T(), suite.user.Created, u.Created, 1*time.Second)
}
Example #12
0
func TestTime(t *testing.T) {

	p, _ := time.Parse(time.RFC3339Nano, "2011-08-18T19:03:37.000000000+01:00")
	data := []struct {
		in  string
		out time.Time
		str string
	}{
		{"2014-12-15T08:00:00.000Z", time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z"},
		{"2011-08-18T19:03:37.000000000+01:00", time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00"},
		{"2014-12-15T19:30:20Z", time.Date(2014, 12, 15, 19, 30, 20, 0, time.UTC), "2014-12-15T19:30:20.000Z"},
	}

	for _, example := range data {
		parsed, err := ParseDateTime(example.in)
		assert.NoError(t, err)
		assert.Equal(t, example.out.String(), parsed.Time.String(), "Failed to parse "+example.in)
		assert.Equal(t, example.str, parsed.String())
		mt, err := parsed.MarshalText()
		assert.NoError(t, err)
		assert.Equal(t, []byte(example.str), mt)
		pp := DateTime{}
		err = pp.UnmarshalText(mt)
		assert.NoError(t, err)
		assert.Equal(t, example.out.String(), pp.Time.String())

		pp = DateTime{}
		err = pp.Scan(example.in)
		assert.NoError(t, err)
		assert.Equal(t, DateTime{example.out}, pp)
	}

	_, err := ParseDateTime("yada")
	assert.Error(t, err)

	parsed, err := ParseDateTime("")
	assert.NoError(t, err)
	assert.WithinDuration(t, time.Unix(0, 0), parsed.Time, 0)

	pp := DateTime{}
	err = pp.UnmarshalText([]byte{})
	assert.NoError(t, err)
	assert.WithinDuration(t, time.Unix(0, 0), pp.Time, 0)
	err = pp.UnmarshalText([]byte("yada"))
	assert.Error(t, err)

	pp = DateTime{}
	err = pp.UnmarshalText([]byte(nil))
	assert.NoError(t, err)
	assert.WithinDuration(t, time.Unix(0, 0), pp.Time, 0)
	err = pp.UnmarshalText([]byte("yada"))
	assert.Error(t, err)
}
Example #13
0
func testBasic(t *testing.T, store data.Store) {
	assert := assert.New(t)
	var users = store.Collection("users")
	assert.NotNil(users)

	// testing basic CRUD operations

	var bob = User{
		Name:  "bob",
		Email: "*****@*****.**",
		Age:   20,
	}

	var err = users.Insert(&bob)
	ok(t, "insert", err)

	if len(bob.ID) == 0 {
		t.Error("ID is not set")
		t.FailNow()
	}

	count, err := users.Count()
	ok(t, "count", err)

	assert.Equal(int64(1), count)

	var now = time.Now().UTC()
	assert.WithinDuration(now, bob.CreatedAt, 500*time.Millisecond, "created_at is not set")
	assert.WithinDuration(now, bob.UpdatedAt, 500*time.Millisecond, "updated_at is not set")

	var usr User
	err = users.Get(bob.ID, &usr)
	ok(t, "get", err)
	assertUser(t, bob, usr)

	bob.Name = "rob"
	bob.Email = "*****@*****.**"
	err = users.Update(bob.ID, &bob)
	ok(t, "update", err)

	err = users.Get(bob.ID, &usr)
	ok(t, "get", err)
	assertUser(t, bob, usr)

	err = users.Delete(bob.ID)
	ok(t, "delete", err)

	count, err = users.Count()
	ok(t, "count", err)

	assert.Equal(int64(0), count)
}
Example #14
0
func TestJobRun(t *testing.T) {
	cache := NewMockCache()

	j := GetMockJobWithGenericSchedule()
	j.Init(cache)
	j.Run(cache)

	now := time.Now()

	assert.Equal(t, j.SuccessCount, uint(1))
	assert.WithinDuration(t, j.LastSuccess, now, 2*time.Second)
	assert.WithinDuration(t, j.LastAttemptedRun, now, 2*time.Second)
}
Example #15
0
// Two Parents with the same child
func TestDependentJobsTwoParentsSameChild(t *testing.T) {
	cache := NewMockCache()

	mockJobOne := GetMockJobWithGenericSchedule()
	mockJobOne.Name = "mock_parent_one"
	mockJobOne.Init(cache)

	mockJobTwo := GetMockJobWithGenericSchedule()
	mockJobTwo.Name = "mock_parent_two"
	mockJobTwo.Init(cache)

	mockChildJob := GetMockJob()
	mockChildJob.Name = "mock_child"
	mockChildJob.ParentJobs = []string{
		mockJobOne.Id,
		mockJobTwo.Id,
	}
	mockChildJob.Init(cache)

	// Check that it gets placed in the array.
	assert.Equal(t, mockJobOne.DependentJobs[0], mockChildJob.Id)
	assert.Equal(t, mockJobTwo.DependentJobs[0], mockChildJob.Id)
	assert.True(t, len(mockJobOne.DependentJobs) == 1)
	assert.True(t, len(mockJobTwo.DependentJobs) == 1)

	parentOne, err := cache.Get(mockJobOne.Id)
	assert.NoError(t, err)
	parentTwo, err := cache.Get(mockJobTwo.Id)
	assert.NoError(t, err)

	// Check that we can still get it from the cache.
	assert.Equal(t, parentOne.DependentJobs[0], mockChildJob.Id)
	assert.Equal(t, parentTwo.DependentJobs[0], mockChildJob.Id)
	assert.True(t, len(parentOne.DependentJobs) == 1)
	assert.True(t, len(parentTwo.DependentJobs) == 1)

	parentOne.Run(cache)
	time.Sleep(time.Second)
	n := time.Now()

	// TODO use abtime
	assert.WithinDuration(t, parentOne.LastAttemptedRun, n, 3*time.Second)
	assert.WithinDuration(t, parentOne.LastSuccess, n, 3*time.Second)
	assert.WithinDuration(t, mockChildJob.LastAttemptedRun, n, 3*time.Second)
	assert.WithinDuration(t, mockChildJob.LastSuccess, n, 3*time.Second)
	assert.True(t, parentTwo.LastAttemptedRun.IsZero())
	assert.True(t, parentTwo.LastSuccess.IsZero())

	// TODO use abtime
	time.Sleep(time.Second * 3)
	parentTwo.Run(cache)
	time.Sleep(time.Second)
	n = time.Now()

	// TODO use abtime
	assert.WithinDuration(t, parentTwo.LastAttemptedRun, n, 3*time.Second)
	assert.WithinDuration(t, parentTwo.LastSuccess, n, 3*time.Second)
	assert.WithinDuration(t, mockChildJob.LastAttemptedRun, n, 3*time.Second)
	assert.WithinDuration(t, mockChildJob.LastSuccess, n, 3*time.Second)
}
Example #16
0
// TestDefaultMeta tests that WorkSpec.Meta gets the correct defaults,
// which in a couple of cases are not zero values.
func TestDefaultMeta(t *testing.T) {
	sts := SimpleTestSetup{
		NamespaceName: "TestDefaultMeta",
		WorkSpecName:  "spec",
	}
	sts.SetUp(t)
	defer sts.TearDown(t)

	meta, err := sts.WorkSpec.Meta(false)
	if assert.NoError(t, err) {
		assert.Equal(t, 0, meta.Priority)
		assert.Equal(t, 20, meta.Weight)
		assert.False(t, meta.Paused)
		assert.False(t, meta.Continuous)
		assert.False(t, meta.CanBeContinuous)
		assert.Zero(t, meta.Interval)
		assert.WithinDuration(t, time.Time{}, meta.NextContinuous, 1*time.Microsecond)
		assert.Equal(t, 0, meta.MaxRunning)
		assert.Equal(t, 0, meta.MaxAttemptsReturned)
		assert.Equal(t, "", meta.NextWorkSpecName)
		assert.Equal(t, 0, meta.AvailableCount)
		assert.Equal(t, 0, meta.PendingCount)
		assert.Equal(t, "", meta.Runtime)
	}
}
Example #17
0
func (check *Check) test(t *testing.T, expect_out string, expect_rc int, message string) {
	if message == "" {
		message = "test_check"
	}
	os.Chmod(check.cmd_args[0], 0755)
	err := check.Spawn()
	if !assert.True(t, check.running, "%s: check.running is true post-spawn", message) {
		t.Fatalf("Couldn't spawn check, bailing out: %s", err.Error())
	}
	assert.NoError(t, err, "%s: no errors on successful spawning of check", message)
	assert.NotNil(t, check.process, "%s: check has a process", message)
	assert.WithinDuration(t, time.Now(), check.started_at, 1*time.Second,
		"%s check started within 1 second from now", message)
	assert.Equal(t, check.ended_at, time.Time{}, "%s: check has no end time (yet)", message)
	assert.False(t, check.sig_term, "%s: check has not been sigtermed", message)
	assert.False(t, check.sig_kill, "%s: check has not been sigkilled", message)

	var finished bool
	for i := 0; i < 100; i++ {
		finished = check.Reap()
		if finished {
			break
		}
		time.Sleep(100 * time.Millisecond)
		assert.Equal(t, "", check.output, "%s: check has no output yet")
	}
	assert.True(t, finished, "%s: check finished", message)
	assert.Equal(t, expect_out, check.output, "%s: check output was as expected", message)
	assert.Equal(t, expect_rc, check.rc, "%s: check rc was as expected", message)
	assert.False(t, check.running, "%s: check is no longer running", message)

}
Example #18
0
func TestGetJob(t *testing.T) {
	testJob := testJobs[0]

	// Expect a HGET operation to be preformed with the job hash key and job ID
	conn.Command("HGET", HashKey, testJob.Job.Id).
		Expect(testJob.Bytes).
		ExpectError(nil)

	storedJob, err := db.Get(testJob.Job.Id)
	assert.Nil(t, err)

	assert.WithinDuration(t, storedJob.NextRunAt, testJob.Job.NextRunAt, 100*time.Microsecond)
	assert.Equal(t, testJob.Job.Name, storedJob.Name)
	assert.Equal(t, testJob.Job.Id, storedJob.Id)
	assert.Equal(t, testJob.Job.Command, storedJob.Command)
	assert.Equal(t, testJob.Job.Schedule, storedJob.Schedule)
	assert.Equal(t, testJob.Job.Owner, storedJob.Owner)
	assert.Equal(t, testJob.Job.Metadata.SuccessCount, storedJob.Metadata.SuccessCount)

	// Test error handling
	conn.Command("HGET", HashKey, testJob.Job.Id).
		ExpectError(errors.New("Redis error"))

	storedJob, err = db.Get(testJob.Job.Id)
	assert.NotNil(t, err)
}
func TestValuePressSource(t *testing.T) {
	f, err := os.Open("data/www.value-press.com/search")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	source := NewValuePressSource()
	feed, err := source.ScrapeFromReader(f)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := time.LoadLocation("Asia/Tokyo")
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, 20, len(feed.Items))
	assert.Equal(t, "ハローキティも出演!世界初!お皿に盛りつけられたコスメを使って楽しむ参加型メイクアップレッスンショー『ミワンダフルのメイクアップレストラン』が開催", feed.Items[0].Title)
	assert.Equal(t, "https://www.value-press.com/pressrelease/171658", feed.Items[0].Link.Href)
	assert.Equal(t, "メイクスマイルプロジェクト所属のメイクスマイルアーティスト・ミワンダフルがお届けする、参加型メイクアップレッスンショー『ミワンダフルのメイクアップレストラン』が2016年11月22(火)、 23日(水・祝)に原宿クエストホールにてオープンします。", feed.Items[0].Description)
	assert.Equal(t, "メイクスマイルプロジェクト", feed.Items[0].Author.Name)
	assert.WithinDuration(t, time.Date(2016, 10, 17, 10, 0, 0, 0, loc), feed.Items[0].Created, 0)
}
Example #20
0
func TestGetJobStats(t *testing.T) {
	ts := NewTestServer()
	defer ts.Close()
	kc := New(ts.URL)
	j := NewJobMap()

	// Create the job
	id, err := kc.CreateJob(j)
	assert.NoError(t, err)
	// Start the job
	ok, err := kc.StartJob(id)
	now := time.Now()
	assert.NoError(t, err)
	assert.True(t, ok)
	// Wait let the job run
	time.Sleep(time.Second * 1)

	stats, err := kc.GetJobStats(id)
	assert.NoError(t, err)
	assert.Equal(t, id, stats[0].JobId)
	assert.Equal(t, uint(0), stats[0].NumberOfRetries)
	assert.True(t, stats[0].Success)
	assert.True(t, stats[0].ExecutionDuration != time.Duration(0))
	assert.WithinDuration(t, now, stats[0].RanAt, time.Second)

	cleanUp()
}
Example #21
0
func TestTwitterSource(t *testing.T) {
	jsonData, err := ioutil.ReadFile("data/api.twitter.com/1.1/statuses/user_timeline.json")
	if err != nil {
		t.Fatal(err)
	}

	var timeline []anaconda.Tweet
	if err := json.Unmarshal(jsonData, &timeline); err != nil {
		t.Fatal(err)
	}

	source := NewTwitterSource(725638238291943424)
	feed, err := source.Render(timeline)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := time.LoadLocation("UTC")
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, "Sanrio Events (@sanrio_events)", feed.Title)
	assert.Equal(t, 20, len(feed.Items))
	assert.Equal(t, "12/24 KT 藤崎本館 (藤崎本館): https://t.co/Gh7HkC6yM2", feed.Items[0].Title)
	assert.Equal(t, "https://twitter.com/sanrio_events/status/805532146110582784", feed.Items[0].Link.Href)
	assert.WithinDuration(t, time.Date(2016, 12, 4, 22, 0, 4, 0, loc), feed.Items[0].Created, 0)
}
Example #22
0
func TestJobRunAndRepeat(t *testing.T) {
	cache := NewMockCache()

	oneSecondFromNow := time.Now().Add(time.Second)
	j := GetMockJobWithSchedule(2, oneSecondFromNow, "PT1S")
	j.Init(cache)

	for i := 0; i < 3; i++ {
		time.Sleep(time.Second)
		now := time.Now()
		assert.Equal(t, j.SuccessCount, uint(i+1))
		assert.WithinDuration(t, j.LastSuccess, now, 3*time.Second)
		assert.WithinDuration(t, j.LastAttemptedRun, now, 3*time.Second)
	}

}
func TestInstagramSource(t *testing.T) {
	f, err := os.Open("data/www.instagram.com/fukkachan628/index.html")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	source := NewInstagramSource("fukkachan628")
	feed, err := source.ScrapeFromReader(f)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := time.LoadLocation("UTC")
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, "ふっかちゃん【公式】", feed.Title)
	assert.Equal(t, "https://www.instagram.com/fukkachan628/", feed.Link.Href)
	assert.Equal(t, 12, len(feed.Items))
	assert.Equal(t, "今年の漢字は「金」!Y(o≧ω≦o)Y\n #今年の漢字 #金 #みんなからもらった金メダル #うれしす #ふっかちゃん #過去のお写真", feed.Items[0].Title)
	assert.Equal(t, "http://www.instagram.com/p/BN59EPyhA09/", feed.Items[0].Link.Href)
	assert.WithinDuration(t, time.Date(2016, 12, 12, 5, 34, 41, 0, loc), feed.Items[0].Created, 0)
}
Example #24
0
func TestGetAllJobs(t *testing.T) {
	// Expect a HVALS operation to be preformed with the job hash key
	conn.Command("HVALS", HashKey).
		Expect([]interface{}{
			testJobs[0].Bytes,
			testJobs[1].Bytes,
			testJobs[2].Bytes,
		}).
		ExpectError(nil)

	jobs, err := db.GetAll()
	assert.Nil(t, err)

	for i, j := range jobs {
		assert.WithinDuration(t, testJobs[i].Job.NextRunAt, j.NextRunAt, 100*time.Microsecond)
		assert.Equal(t, testJobs[i].Job.Name, j.Name)
		assert.Equal(t, testJobs[i].Job.Id, j.Id)
		assert.Equal(t, testJobs[i].Job.Command, j.Command)
		assert.Equal(t, testJobs[i].Job.Schedule, j.Schedule)
		assert.Equal(t, testJobs[i].Job.Owner, j.Owner)
		assert.Equal(t, testJobs[i].Job.Metadata.SuccessCount, j.Metadata.SuccessCount)
	}

	// Test erorr handling
	conn.Command("HVALS", HashKey).
		ExpectError(errors.New("Redis error"))

	jobs, err = db.GetAll()
	assert.NotNil(t, err)
}
Example #25
0
// Parent with two childs
func TestDependentJobsTwoChilds(t *testing.T) {
	cache := NewMockCache()

	mockJob := GetMockJobWithGenericSchedule()
	mockJob.Name = "mock_parent_job"
	mockJob.Init(cache)

	mockChildJobOne := GetMockJob()
	mockChildJobOne.Name = "mock_child_one"
	mockChildJobOne.ParentJobs = []string{
		mockJob.Id,
	}
	mockChildJobOne.Init(cache)

	mockChildJobTwo := GetMockJob()
	mockChildJobTwo.Name = "mock_child_two"
	mockChildJobTwo.ParentJobs = []string{
		mockJob.Id,
	}
	mockChildJobTwo.Init(cache)

	// Check that it gets placed in the array.
	assert.Equal(t, mockJob.DependentJobs[0], mockChildJobOne.Id)
	assert.Equal(t, mockJob.DependentJobs[1], mockChildJobTwo.Id)
	assert.True(t, len(mockJob.DependentJobs) == 2)

	j, err := cache.Get(mockJob.Id)
	assert.NoError(t, err)

	// Check that we can still get it from the cache.
	assert.Equal(t, j.DependentJobs[0], mockChildJobOne.Id)
	assert.Equal(t, j.DependentJobs[1], mockChildJobTwo.Id)
	assert.True(t, len(j.DependentJobs) == 2)

	j.Run(cache)
	time.Sleep(time.Second * 2)
	n := time.Now()

	// TODO use abtime
	assert.WithinDuration(t, mockChildJobOne.LastAttemptedRun, n, 4*time.Second)
	assert.WithinDuration(t, mockChildJobOne.LastSuccess, n, 4*time.Second)
	assert.WithinDuration(t, mockChildJobTwo.LastAttemptedRun, n, 4*time.Second)
	assert.WithinDuration(t, mockChildJobTwo.LastSuccess, n, 4*time.Second)

	// Test the fact that the dependent jobs follow a rule of FIFO
	assert.True(t, mockChildJobOne.LastAttemptedRun.UnixNano() < mockChildJobTwo.LastAttemptedRun.UnixNano())
}
Example #26
0
func TestWait(t *testing.T) {
	b := &SimpleBackoff{
		Factor:      2,
		Min:         100 * time.Millisecond,
		Max:         2 * time.Second,
		MaxAttempts: 1,
	}
	t1 := time.Now()
	err1 := Wait(b)
	t2 := time.Now()
	err2 := Wait(b)
	t3 := time.Now()
	assert.WithinDuration(t, t1.Add(100*time.Millisecond), t2, time.Millisecond)
	assert.WithinDuration(t, t2, t3, time.Millisecond)
	assert.Nil(t, err1)
	assert.NotNil(t, err2)

}
Example #27
0
func assertItemEqual(t *testing.T, a, b models.Item) {
	assert.Equal(t, a.Body, b.Body)
	assert.Equal(t, a.PermaLink, b.PermaLink)
	assert.WithinDuration(t, a.PubDate.Time, b.PubDate.Time, time.Second)
	assert.Equal(t, a.Title, b.Title)
	assert.Equal(t, a.Link, b.Link)
	assert.Equal(t, a.Id, b.Id)
	assert.Equal(t, a.Comments, b.Comments)
	assert.Equal(t, a.Enclosures, b.Enclosures)
}
Example #28
0
func TestTiming(t *testing.T) {
	start := time.Now()
	stat := NewStat(nil, GroupByValue("request"))
	assert.WithinDuration(t, start, stat.StartedAt, time.Duration(time.Millisecond),
		"Constructor should setup StartedAt")
	assert.Equal(t, stat.ElapsedTime, 0)
	elapsed := stat.Stop()
	assert.NotEqual(t, stat.ElapsedTime, 0)
	assert.Equal(t, stat.ElapsedTime, elapsed)
}
Example #29
0
func TestBuildDefaultsCreatedAtToNowOnParseError(t *testing.T) {
	_, builder := buildAlertBuilder()
	inputAlert := buildMonitAlert()
	inputAlert.Date = "Thu, 02 May 2013 20:07:0"

	builtAlert, _ := builder.Build(inputAlert)

	createdAt := time.Unix(builtAlert.CreatedAt, 0)
	now := time.Now()

	assert.WithinDuration(t, now, createdAt, 1*time.Second)
}
Example #30
0
func TestRiver(t *testing.T) {
	db := memdata.Open()

	r := New(db, Options{})

	var buf bytes.Buffer
	r.WriteTo(&buf)

	var v models.River
	json.Unmarshal(buf.Bytes(), &v)

	assert := assert.New(t)

	assert.Equal(docsPath, v.Metadata.Docs)
	assert.WithinDuration(time.Now(), v.Metadata.WhenGMT.Time, time.Second)
	assert.WithinDuration(time.Now(), v.Metadata.WhenLocal.Time, time.Second)
	assert.Equal("3", v.Metadata.Version)
	assert.Equal(float64(0), v.Metadata.Secs)

	assert.Equal(models.Feeds{[]models.Feed{}}, v.UpdatedFeeds)
}