Beispiel #1
0
func TestDynamoBasic(t *testing.T) {
	ctx := core.NewContext("test")

	ddb, err := NewStorage(ctx, testConfig)
	if noConnection(err) {
		t.Skip()
	}

	if err != nil {
		t.Fatal(err)
	}
	server := ddb.server

	{
		tables, err := server.ListTables()
		if err != nil {
			t.Fatal(err)
		}

		for _, t := range tables {
			log.Println("table " + t)
		}
	}

	table := ddb.table

	{
		attrs := []dynamodb.Attribute{
			*dynamodb.NewStringAttribute("likes", "beer"),
		}

		if ok, err := table.PutItem("homer", "", attrs); !ok {
			t.Fatal(err)
		}
	}

	{
		attrs := []dynamodb.Attribute{
			*dynamodb.NewStringAttribute("likes", "tacos"),
		}

		k := dynamodb.Key{HashKey: "homer"}
		if ok, err := table.UpdateAttributes(&k, attrs); !ok {
			t.Fatal(err)
		}
	}

	{
		k := dynamodb.Key{HashKey: "homer"}
		as, err := table.GetItem(&k)
		if err != nil {
			t.Fatal(err)
		}
		for _, v := range as {
			log.Println(v)
		}
	}
}
Beispiel #2
0
func TestDynamoThroughput(t *testing.T) {
	location := "there"
	n := 100

	ctx := core.NewContext(location)
	store, err := NewStorage(ctx, testConfig)
	if noConnection(err) {
		t.Skip()
	}
	if err != nil {
		t.Fatal(err)
	}

	state, err := core.NewLinearState(ctx, location, store)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := core.NewLocation(ctx, location, state, nil)
	if err != nil {
		t.Fatal(err)
	}

	loc.Clear(ctx)

	then := time.Now().UTC().UnixNano()
	for i := 0; i < n; i++ {
		fact := fmt.Sprintf(`{"likes":"beer %d"}`, i)
		_, err := loc.AddFact(ctx, "", core.MustMap(fact))
		if err != nil {
			// ProvisionedThroughputExceededException: The
			// level of configured provisioned throughput
			// for the table was exceeded. Consider
			// increasing your provisioning level with the
			// UpdateTable API
			log.Printf("Died at iteration %d", i)
			log.Fatal(err)
		}
	}
	elapsed := time.Now().UTC().UnixNano() - then
	log.Printf("elapsed %d us", elapsed/1000)

	then = time.Now().UTC().UnixNano()
	sr, err := loc.SearchFacts(ctx, core.MustMap(`{"likes":"?x"}`), false)
	if err != nil {
		log.Fatal(err)
	}
	elapsed = time.Now().UTC().UnixNano() - then
	log.Printf("elapsed %d us", elapsed/1000)
	log.Printf("# SearchFacts found %d\n", len(sr.Found))
}
Beispiel #3
0
func TestCron(t *testing.T) {
	ctx := core.NewContext("test")
	log.Println("TestCron")

	c, err := NewCron(nil, 1*time.Second, "test", 10)
	if err != nil {
		t.Fatal(err)
	}

	then := time.Now().Add(2 * time.Second).Format(time.RFC3339)
	log.Printf("then: %s", then)
	c.Add(ctx, "1", "!"+then, func(t time.Time) error {
		fmt.Printf("RUNNING 1 AT %v\n", t)
		return nil
	})

	c.Add(ctx, "2", "1-59/2 * * * * * *", func(t time.Time) error {
		fmt.Printf("RUNNING 2 AT %v\n", t)
		return nil
	})

	c.Add(ctx, "3", "+3s", func(t time.Time) error {
		fmt.Printf("RUNNING 3 AT %v\n", t)
		return nil
	})

	counter := 0
	last := time.Now()
	c.Add(ctx, "4", "1-59/2 * * * * * *", func(t time.Time) error {
		now := time.Now()
		delta := now.Sub(last)
		last = now
		fmt.Printf("RUNNING 4 AT %v (counter %d, elapsed %v)\n", t, counter, delta)
		counter++
		return nil
	})

	time.Sleep(6 * time.Second)
	c.Rem(ctx, "2")

	time.Sleep(6 * time.Second)

	SysCronBroadcaster.Suspend()
	time.Sleep(3 * time.Second)

	SysCronBroadcaster.Resume()
	time.Sleep(3 * time.Second)

	c.Rem(ctx, "4")
}
Beispiel #4
0
func TestDynamoFacts(t *testing.T) {
	location := "there"

	ctx := core.NewContext("test")

	store, err := NewStorage(ctx, testConfig)
	if noConnection(err) {
		t.Skip()
	}
	if err != nil {
		t.Fatal(err)
	}

	state, err := core.NewLinearState(ctx, location, store)
	if err != nil {
		t.Fatal(err)
	}

	loc, err := core.NewLocation(ctx, location, state, nil)
	if err != nil {
		t.Fatal(err)
	}

	sr, err := loc.SearchFacts(ctx, core.MustMap(`{"likes":"?x"}`), false)
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("# SearchFacts %v %v\n", *sr, err)

	loc.Clear(ctx)

	id, err := loc.AddFact(ctx, "", core.MustMap(`{"likes":"chips"}`))
	log.Printf("# AddFact %s %v\n", id, err)
	id, err = loc.AddFact(ctx, "", core.MustMap(`{"likes":"beer"}`))
	log.Printf("# AddFact %s %v\n", id, err)
	id, err = loc.AddFact(ctx, "", core.MustMap(`{"wants":"tacos"}`))
	log.Printf("# AddFact %s %v\n", id, err)
	id, err = loc.AddFact(ctx, "", core.MustMap(`{"wants":"tacos"}`))
	log.Printf("# AddFact %s %v\n", id, err)

	sr, err = loc.SearchFacts(ctx, core.MustMap(`{"wants":"?x"}`), false)
	log.Printf("# SearchFacts 1 %v %v\n", *sr, err)

	id, err = loc.RemFact(ctx, id)
	log.Printf("# RemFact %v %v\n", id, err)

	sr, err = loc.SearchFacts(ctx, core.MustMap(`{"wants":"?x"}`), false)
	log.Printf("# SearchFacts 2 %v %v\n", *sr, err)
}
Beispiel #5
0
func TestCronResume(t *testing.T) {
	ctx := core.NewContext("test")
	log.Println("TestCron1")

	c, err := NewCron(nil, 1*time.Second, "test", 10)
	if err != nil {
		t.Fatal(err)
	}

	//create "1"
	counter := 0
	last := time.Now()
	c.Add(ctx, "1", "1-59/1 * * * * * *", func(t time.Time) error {
		now := time.Now()
		delta := now.Sub(last)
		last = now
		fmt.Printf("RUNNING 1 AT %v (counter %d, elapsed %v)\n", t, counter, delta)
		counter++
		return nil
	})

	time.Sleep(6 * time.Second)

	//suspend all
	SysCronBroadcaster.Suspend()

	c2, err := NewCron(nil, 1*time.Second, "test", 10)
	if err != nil {
		t.Fatal(err)
	}

	//create "2" which should be suspeneded
	counter2 := 0
	last2 := time.Now()
	c2.Add(ctx, "2", "1-59/1 * * * * * *", func(t time.Time) error {
		now := time.Now()
		delta := now.Sub(last2)
		last2 = now
		fmt.Printf("RUNNING 2 AT %v (counter %d, elapsed %v)\n", t, counter2, delta)
		counter2++
		return nil
	})

	time.Sleep(5 * time.Second)

	//resume only "2"
	c2.Resume(ctx)

	time.Sleep(5 * time.Second)

	//suspend all again
	SysCronBroadcaster.Suspend()

	time.Sleep(5 * time.Second)

	//resume all
	SysCronBroadcaster.Resume()
	time.Sleep(3 * time.Second)

	c.Rem(ctx, "1")
	c2.Rem(ctx, "2")
}
Beispiel #6
0
func TestCronUTC(t *testing.T) {
	// Attempt to test that scheduled cron jobs run on UTC time.

	ctx := core.NewContext("test")

	c, err := NewCron(nil, 1*time.Second, "test", 10)
	if err != nil {
		t.Fatal(err)
	}
	c.Start(ctx)

	// We'll listen on this channel to learn what's happened.
	ch := make(chan bool)
	then := time.Now()
	fn := func(now time.Time) error {
		fmt.Printf("CronUTC fn %v (elapsed %v)\n", now, now.Sub(then))
		// Report that this job executed.
		ch <- true
		return nil
	}

	// What time is it now?
	now := time.Now().UTC()
	hour := now.Hour()
	min := now.Minute()
	sec := now.Second()
	fmt.Printf("CronUTC %02d:%02d:%02d (%v)\n", hour, min, sec, now)

	// Make a schedule to fire 10 seconds from now.
	sec += 10
	if 60 <= sec {
		sec = 0
		min++
	}
	if 60 <= min {
		min = 0
		hour++
	}
	if 24 <= hour {
		hour = 0
	}
	// That logic will fail to catch the situation when local time
	// is ahead of UTC and the code has a certain bug.

	// This job should execute within 10 seconds.
	schedule := fmt.Sprintf("%d %d %d * * * *", sec, min, hour)
	fmt.Printf("CronUTC schedule '%s'\n", schedule)
	if err = c.Add(ctx, "test", schedule, fn); err != nil {
		t.Fatal(err)
	}

	// Wait long enough for the job to execute.  Hopefully.
	go func() {
		time.Sleep(1 * time.Minute)
		// Report that we, not the job, are responding.
		ch <- false
	}()

	got := <-ch
	if !got {
		t.Fatalf("heard %v, which wasn't from the job", got)
	}
}