Example #1
0
func (me *localSingleAppTestSuite) TestMyMarshaller() {
	fn := func(n int, name string) (msg string, count int) {
		msg = name + "_'s message"
		count = n + 1
		return
	}

	// register marshaller
	mid := int(101)
	err := me.app.AddMarshaller(mid, &struct {
		testMyInvoker
		testMyMarshaller
	}{})
	me.Nil(err)

	// allocate workers
	me.Nil(me.app.Register("TestMyMarshaller", fn))
	me.Nil(me.app.SetMarshaller("TestMyMarshaller", mid, mid))
	remain, err := me.app.Allocate("TestMyMarshaller", 1, 1)
	me.Equal(0, remain)
	me.Nil(err)

	reports, err := me.app.Call(
		"TestMyMarshaller",
		dingo.DefaultOption(),
		12345, "mission",
	)
	me.Nil(err)
	r := <-reports
	me.Equal("mission_'s message", r.Return()[0].(string))
	me.Equal(int(12346), r.Return()[1].(int))
}
Example #2
0
func TestReportMarshal(t *testing.T) {
	ass := assert.New(t)

	body, err := json.Marshal(&dingo.Report{
		H: dingo.NewHeader("test_id", "test_name"),
		P: &dingo.ReportPayload{
			S: 101,
			E: &dingo.Error{102, "test error"},
			O: dingo.DefaultOption().IgnoreReport(true).MonitorProgress(true),
			R: nil,
		},
	})
	ass.Nil(err)

	var r dingo.Report
	err = json.Unmarshal(body, &r)
	ass.Nil(err)
	if err == nil {
		ass.Equal(int16(101), r.Status())
		ass.Equal("test_id", r.ID())
		ass.Equal(int32(102), r.Error().Code())
		ass.Equal("test error", r.Error().Msg())
		ass.Equal(true, r.Option().GetIgnoreReport())
		ass.Equal(true, r.Option().GetMonitorProgress())
	}
}
Example #3
0
func (ts *DingoMultiAppTestSuite) TestOrder() {
	countOfTasks := 5
	work := func(n int, name string) (int, string) {
		return n + 1, name + "b"
	}

	// register worker function
	ts.register("TestOrder", work)
	ts.setOption("TestOrder", dingo.DefaultOption().MonitorProgress(true))
	ts.allocate("TestOrder", 1, 1)

	// sending tasks
	reports := [][]<-chan *dingo.Report{}
	for k, v := range ts.Callers {
		rs := []<-chan *dingo.Report{}
		for i := 0; i < countOfTasks; i++ {
			rep, err := v.Call("TestOrder", nil, k, fmt.Sprintf("%d.%d", k, i))
			ts.Nil(err)
			if err != nil {
				return
			}

			rs = append(rs, rep)
		}
		reports = append(reports, rs)
	}

	// check results one by one
	for k, rs := range reports {
		for i, v := range rs {
			// sent
			rep := <-v
			ts.Equal(dingo.Status.Sent, rep.Status())
			name, id := rep.Name(), rep.ID()

			// progress
			rep = <-v
			ts.Equal(dingo.Status.Progress, rep.Status())
			ts.Equal(name, rep.Name())
			ts.Equal(id, rep.ID())

			// success
			rep = <-v
			ts.Equal(dingo.Status.Success, rep.Status())
			ts.Equal(name, rep.Name())
			ts.Equal(id, rep.ID())

			// check result
			ret := rep.Return()
			ts.Len(ret, 2)
			if len(ret) == 2 {
				// plus 1
				ts.Equal(k+1, ret[0].(int))
				// plus 'b'
				ts.Equal(fmt.Sprintf("%d.%db", k, i), ret[1].(string))
			}
		}
	}
}
Example #4
0
func (ts *DingoSingleAppTestSuite) TestBasic() {
	// register a set of workers
	called := 0
	err := ts.app.Register("TestBasic",
		func(n int) int {
			called = n
			return n + 1
		},
	)
	ts.Nil(err)
	remain, err := ts.app.Allocate("TestBasic", 1, 1)
	ts.Nil(err)
	ts.Equal(0, remain)

	// call that function
	reports, err := ts.app.Call("TestBasic", dingo.DefaultOption().MonitorProgress(true), 5)
	ts.Nil(err)
	ts.NotNil(reports)

	// await for reports
	status := []int16{
		dingo.Status.Sent,
		dingo.Status.Progress,
		dingo.Status.Success,
	}
	for {
		done := false
		select {
		case v, ok := <-reports:
			ts.True(ok)
			if !ok {
				break
			}

			// make sure the order of status is right
			ts.True(len(status) > 0)
			if len(status) > 0 {
				ts.Equal(status[0], v.Status())
				status = status[1:]
			}

			if v.Done() {
				ts.Equal(5, called)
				ts.Len(v.Return(), 1)
				if len(v.Return()) > 0 {
					ret, ok := v.Return()[0].(int)
					ts.True(ok)
					ts.Equal(called+1, ret)
				}
				done = true
			}
		}

		if done {
			break
		}
	}
}
Example #5
0
func (me *localSingleAppTestSuite) TestCustomMarshaller() {
	fn := func(n int, name string) (msg string, count int) {
		msg = name + "_'s message"
		count = n + 1
		return
	}

	// register marshaller
	mid := int(102)
	err := me.app.AddMarshaller(mid, &struct {
		testMyInvoker
		dingo.CustomMarshaller
	}{
		testMyInvoker{},
		dingo.CustomMarshaller{Codec: &testMyCodec{}},
	})
	me.Nil(err)

	// allocate workers
	me.Nil(me.app.Register("TestCustomMarshaller", fn))
	me.Nil(me.app.SetMarshaller("TestCustomMarshaller", mid, mid))
	remain, err := me.app.Allocate("TestCustomMarshaller", 1, 1)
	me.Equal(0, remain)
	me.Nil(err)

	// initiate a task with an option(IgnoreReport == true)
	reports, err := me.app.Call(
		"TestCustomMarshaller", dingo.DefaultOption().MonitorProgress(true), 12345, "mission",
	)
	me.Nil(err)

finished:
	for {
		select {
		case r := <-reports:
			if r.OK() {
				me.Equal("mission_'s message", r.Return()[0].(string))
				me.Equal(int(12346), r.Return()[1].(int))
			}
			if r.Fail() {
				me.Fail("this task is failed, unexpected")
			}
			if r.Done() {
				break finished
			}
		}
	}
}
Example #6
0
func (me *localSingleAppTestSuite) TestCustomMarshallerWithMinimalFunc() {
	called := false
	fn := func() {
		called = true
	}

	// register marshaller
	mid := int(103)
	err := me.app.AddMarshaller(mid, &struct {
		testMyInvoker2
		dingo.CustomMarshaller
	}{
		testMyInvoker2{},
		dingo.CustomMarshaller{Codec: &testMyMinimalCodec{}},
	})
	me.Nil(err)

	// allocate workers
	me.Nil(me.app.Register("TestCustomMarshallerWithMinimalFunc", fn))
	me.Nil(me.app.SetMarshaller("TestCustomMarshallerWithMinimalFunc", mid, mid))
	remain, err := me.app.Allocate("TestCustomMarshallerWithMinimalFunc", 1, 1)
	me.Equal(0, remain)
	me.Nil(err)

	// initiate a task with an option(IgnoreReport == true)
	reports, err := me.app.Call(
		"TestCustomMarshallerWithMinimalFunc", dingo.DefaultOption().MonitorProgress(true),
	)
	me.Nil(err)

finished:
	for {
		select {
		case r := <-reports:
			if r.OK() {
				me.Len(r.Return(), 0)
			}
			if r.Fail() {
				me.Fail("this task is failed, unexpected")
			}
			if r.Done() {
				break finished
			}
		}
	}
	me.True(called)
}
Example #7
0
func (me *localSingleAppTestSuite) TestIgnoreReport() {
	// initiate workers
	me.Nil(me.app.Register(
		"TestIgnoreReport", func() {},
	))
	remain, err := me.app.Allocate("TestIgnoreReport", 1, 1)
	me.Equal(0, remain)
	me.Nil(err)

	// initiate a task with an option(IgnoreReport == true)
	reports, err := me.app.Call(
		"TestIgnoreReport",
		dingo.DefaultOption().IgnoreReport(true).MonitorProgress(true),
	)
	me.Nil(err)
	me.Nil(reports)
}
Example #8
0
func (ts *DingoSingleAppTestSuite) TestSameID() {
	// tasks of different name have the same IDs
	var (
		err                error
		countOfConcurrency = 12
		countOfTasks       = 5
		results            = make([][]*dingo.Result, countOfConcurrency)
		wait               sync.WaitGroup
	)
	defer func() {
		ts.Nil(err)
	}()
	fn := func(n int, msg string) (int, string) {
		return n + 1000, "the msg: " + msg
	}

	for i := 0; i < countOfConcurrency; i++ {
		name := fmt.Sprintf("TestSameID.%d", i)
		err = ts.app.Register(name, fn)
		if err != nil {
			return
		}
		err = ts.app.AddIDMaker(100+i, &dingo.SeqIDMaker{})
		if err != nil {
			return
		}
		err = ts.app.SetIDMaker(name, 100+i)
		if err != nil {
			return
		}
		err = ts.app.SetOption(name, dingo.DefaultOption().MonitorProgress(true))
		if err != nil {
			return
		}
		_, err = ts.app.Allocate(name, 5, 3)
		if err != nil {
			return
		}
	}

	// send tasks in parellel
	wait.Add(countOfConcurrency)
	{
		for i := 0; i < countOfConcurrency; i++ {
			go func(idx int, name string) {
				defer wait.Done()
				for j := 0; j < countOfTasks; j++ {
					results[idx] = append(
						results[idx],
						dingo.NewResult(
							ts.app.Call(
								name, nil,
								idx*countOfConcurrency+j, fmt.Sprintf("%d", j),
							)))
				}
			}(i, fmt.Sprintf("TestSameID.%d", i))
		}
	}
	wait.Wait()

	// receiving reports
	received := 0
	defer func() {
		ts.Equal(countOfConcurrency*countOfTasks, received)
	}()
	for i, v := range results {
		for j, r := range v {
			err = r.Wait(0)
			if err != nil {
				return
			}

			ts.Equal(i*countOfConcurrency+j+1000, r.Last.Return()[0].(int))
			ts.Equal(fmt.Sprintf("the msg: %d", j), r.Last.Return()[1].(string))
			received++
		}
	}
}