Пример #1
0
// Init sets up the node for action.  It creates a pipe and adaptor for this node,
// and then recurses down the tree calling Init on each child
func (n *Node) Init(interval time.Duration) (err error) {
	path := n.Path()
	if n.Parent == nil { // we don't have a parent, we're the source
		n.pipe = pipe.NewPipe(nil, path)
	} else { // we have a parent, so pass in the parent's pipe here
		n.pipe = pipe.NewPipe(n.Parent.pipe, path)
	}

	n.adaptor, err = adaptor.Createadaptor(n.Type, path, n.Extra, n.pipe)
	if err != nil {
		return err
	}

	for _, child := range n.Children {
		err = child.Init(interval) // init each child
		if err != nil {
			return err
		}
	}

	return nil
}
func BenchmarkTransformOne(b *testing.B) {
	tpipe := pipe.NewPipe(nil, "path")
	transformer := &Transformer{
		pipe: tpipe,
		path: "path",
		fn:   "module.exports=function(doc) { return doc }",
	}
	err := transformer.initEnvironment()
	if err != nil {
		panic(err)
	}

	msg := message.NewMsg(message.Insert, map[string]interface{}{"id": bson.NewObjectId(), "name": "nick"})
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		transformer.transformOne(msg)
	}
}
Пример #3
0
func TestCreateadaptor(t *testing.T) {
	Register("testadaptor", "description", NewTestadaptor, struct{}{})

	data := []struct {
		kind  string
		extra Config
		out   *Testadaptor
		err   string
	}{
		{
			"testadaptor",
			Config{"value": "rockettes"},
			&Testadaptor{value: "rockettes"},
			"",
		},
		{
			"testadaptor",
			Config{"blah": "rockettes"},
			&Testadaptor{},
			"cannot create testadaptor adaptor (a/b/c). this is an error",
		},
		{
			"notasource",
			Config{"blah": "rockettes"},
			nil,
			"adaptor 'notasource' not found in registry",
		},
	}
	for _, v := range data {
		adaptor, err := Createadaptor(v.kind, "a/b/c", v.extra, pipe.NewPipe(nil, "some name"))

		if err != nil && err.Error() != v.err {
			t.Errorf("\nexpected error: `%v`\ngot error: `%v`\n", v.err, err.Error())
			t.FailNow()
		}
		if !reflect.DeepEqual(v.out, adaptor) && err == nil {
			t.Errorf("expected:\n%+v\ngot:\n%+v\n", v.out, adaptor)
		}
	}
}
func TestTransformOne(t *testing.T) {
	bsonID1 := bson.NewObjectId()
	bsonID2 := bson.ObjectIdHex("54a4420502a14b9641000001")
	tpipe := pipe.NewPipe(nil, "path")
	go func(p *pipe.Pipe) {
		for range p.Err {
			// noop
		}
	}(tpipe)

	data := []struct {
		fn  string
		in  *message.Msg
		out *message.Msg
		err bool
	}{
		{
			// just pass through
			"module.exports=function(doc) { return doc }",
			message.NewMsg(message.Insert, map[string]interface{}{"id": "id1", "name": "nick"}),
			message.NewMsg(message.Insert, map[string]interface{}{"id": "id1", "name": "nick"}),
			false,
		},
		{
			// delete the 'name' property
			"module.exports=function(doc) { doc['data'] = _.omit(doc['data'], ['name']); return doc }",
			message.NewMsg(message.Insert, map[string]interface{}{"id": "id2", "name": "nick"}),
			message.NewMsg(message.Insert, map[string]interface{}{"id": "id2"}),
			false,
		},
		{
			// delete's should be processed the same
			"module.exports=function(doc) { doc['data'] =  _.omit(doc['data'], ['name']); return doc }",
			message.NewMsg(message.Delete, map[string]interface{}{"id": "id2", "name": "nick"}),
			message.NewMsg(message.Delete, map[string]interface{}{"id": "id2"}),
			false,
		},
		{
			// delete's and commands should pass through, and the transformer fn shouldn't run
			"module.exports=function(doc) { return _.omit(doc['data'], ['name']) }",
			message.NewMsg(message.Command, map[string]interface{}{"id": "id2", "name": "nick"}),
			message.NewMsg(message.Command, map[string]interface{}{"id": "id2", "name": "nick"}),
			false,
		},
		{
			// bson should marshal and unmarshal properly
			"module.exports=function(doc) { return doc }",
			message.NewMsg(message.Insert, map[string]interface{}{"id": bsonID1, "name": "nick"}),
			message.NewMsg(message.Insert, map[string]interface{}{"id": bsonID1, "name": "nick"}),
			false,
		},
		{
			// we should be able to change the bson
			"module.exports=function(doc) { doc['data']['id']['$oid'] = '54a4420502a14b9641000001'; return doc }",
			message.NewMsg(message.Insert, map[string]interface{}{"id": bsonID1, "name": "nick"}),
			message.NewMsg(message.Insert, map[string]interface{}{"id": bsonID2, "name": "nick"}),
			false,
		},
		{
			// this throws an error
			"module.exports=function(doc) { return doc['data']['name'] }",
			message.NewMsg(message.Insert, map[string]interface{}{"id": bsonID1, "name": "nick"}),
			message.NewMsg(message.Insert, "nick"),
			true,
		},
	}
	for _, v := range data {

		transformer := &Transformer{pipe: tpipe, path: "path", fn: v.fn}
		err := transformer.initEnvironment()
		if err != nil {
			panic(err)
		}

		msg, err := transformer.transformOne(v.in)
		if (err != nil) != v.err {
			t.Errorf("error expected %t but actually got %v", v.err, err)
			continue
		}
		if (!reflect.DeepEqual(msg.Data, v.out.Data) || err != nil) && !v.err {
			t.Errorf("expected:\n(%T) %+v\ngot:\n(%T) %+v with error (%v)\n", v.out.Data, v.out.Data, msg.Data, msg.Data, err)
		}
	}
}