Exemplo n.º 1
0
func assertChannelContainsMessage(a *assert.Assertions, c <-chan *protocol.Message, msg []byte) {
	select {
	case m := <-c:
		a.Equal(string(msg), string(m.Body))
	case <-time.After(time.Millisecond * 5):
		a.Fail("No message received")
	}
}
Exemplo n.º 2
0
// ExpectDone waits to receive a value in the doneChannel for at least a second
// or fails the test.
func ExpectDone(a *assert.Assertions, doneChannel chan bool) {
	select {
	case <-doneChannel:
		return
	case <-time.After(time.Second):
		a.Fail("timeout in expectDone")
	}
}
Exemplo n.º 3
0
func assertChannelContainsMessage(a *assert.Assertions, c chan MsgAndRoute, msg []byte) {
	//log.Println("DEBUG: start assertChannelContainsMessage-> select")
	select {
	case msgBack := <-c:
		a.Equal(string(msg), string(msgBack.Message.Body))
	case <-time.After(time.Millisecond):
		a.Fail("No message received")
	}
}
func messagePartitionReader(name string, a *assert.Assertions, store *MessagePartition, n int, done chan bool) {
	lastReadMessage := 0
	for lastReadMessage < n {

		msgC := make(chan MessageAndId)
		errorC := make(chan error)

		log.WithFields(log.Fields{
			"module":      "testing",
			"name":        name,
			"lastReadMsg": lastReadMessage + 1,
		}).Debug("Start fetching at")

		store.Fetch(FetchRequest{
			Partition:     "myMessages",
			StartId:       uint64(lastReadMessage + 1),
			Direction:     1,
			Count:         math.MaxInt32,
			MessageC:      msgC,
			ErrorCallback: errorC,
			StartCallback: make(chan int, 1),
		})

	fetch:

		for {
			select {
			case msgAndId, open := <-msgC:
				if !open {

					log.WithFields(log.Fields{
						"module":      "testing",
						"name":        name,
						"lastReadMsg": lastReadMessage,
					}).Debug("Stop fetching at")

					break fetch
				}
				a.Equal(lastReadMessage+1, int(msgAndId.Id))
				lastReadMessage = int(msgAndId.Id)
			case err := <-errorC:
				a.Fail("received error", err.Error())
				<-done
				return
			}
		}

	}
	log.WithFields(log.Fields{
		"module":      "testing",
		"name":        name,
		"lastReadMsg": lastReadMessage,
	}).Debug("Ready got id")

	done <- true
}
Exemplo n.º 5
0
func expectMessages(a *assert.Assertions, msgChannel chan []byte, message ...string) {
	for _, m := range message {
		select {
		case msg := <-msgChannel:
			a.Equal(m, string(msg))
		case <-time.After(time.Millisecond * 100):
			a.Fail("timeout: " + m)
		}
	}
}
Exemplo n.º 6
0
func loadJson(filename string, a *assert.Assertions) map[string]string {
	var data map[string]string
	content, err := ioutil.ReadFile("./test.json")

	if err != nil {
		a.Fail("Failed to read file ", err)
	}

	json.Unmarshal(content, &data)
	return data
}
func messagePartitionReader(name string, a *assert.Assertions, mStore *messagePartition, n int, done chan bool) {
	lastReadMessage := 0

	for lastReadMessage < n {
		msgC := make(chan *store.FetchedMessage, 10)
		errorC := make(chan error)

		log.WithFields(log.Fields{
			"module":      "testing",
			"name":        name,
			"lastReadMsg": lastReadMessage + 1,
		}).Debug("Start fetching")

		mStore.Fetch(&store.FetchRequest{
			Partition: "myMessages",
			StartID:   uint64(lastReadMessage + 1),
			Direction: 1,
			Count:     math.MaxInt32,
			MessageC:  msgC,
			ErrorC:    errorC,
			StartC:    make(chan int, 1),
		})

	FETCH:
		for {
			select {
			case msgAndID, open := <-msgC:
				if !open {
					log.WithFields(log.Fields{
						"module":      "testing",
						"name":        name,
						"lastReadMsg": lastReadMessage,
					}).Debug("Stop fetching")
					break FETCH
				}
				a.Equal(lastReadMessage+1, int(msgAndID.ID), "Reader: "+name)
				lastReadMessage = int(msgAndID.ID)
			case err := <-errorC:
				a.Fail("received error", err.Error())
				<-done
				return
			}
		}
	}

	log.WithFields(log.Fields{
		"module":      "testing",
		"name":        name,
		"lastReadMsg": lastReadMessage,
	}).Debug("Ready got id")

	done <- true
}
Exemplo n.º 8
0
// deepCheckValue checks that all given keys correspond to a valid path in the
// configuration map of the given layer, and that the final value equals the one given
func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value interface{}) {
	if assert == nil || v == nil ||
		len(keys) == 0 || len(keys[0]) == 0 {
		return
	}

	// init
	var val interface{}
	var ms string
	switch l {
	case defaultLayer:
		val = v.defaults
		ms = "v.defaults"
	case overrideLayer:
		val = v.override
		ms = "v.override"
	}

	// loop through map
	var m map[string]interface{}
	err := false
	for _, k := range keys {
		if val == nil {
			assert.Fail(fmt.Sprintf("%s is not a map[string]interface{}", ms))
			return
		}

		// deep scan of the map to get the final value
		switch val.(type) {
		case map[interface{}]interface{}:
			m = cast.ToStringMap(val)
		case map[string]interface{}:
			m = val.(map[string]interface{})
		default:
			assert.Fail(fmt.Sprintf("%s is not a map[string]interface{}", ms))
			return
		}
		ms = ms + "[\"" + k + "\"]"
		val = m[k]
	}
	if !err {
		assert.Equal(value, val)
	}
}
Exemplo n.º 9
0
func asserChannelContainsEntries(a *assert.Assertions, entryC chan [2]string, expectedEntries ...[2]string) {
	allEntries := make([][2]string, 0)

loop:
	for {
		select {
		case entry, ok := <-entryC:
			if !ok {
				break loop
			}
			allEntries = append(allEntries, entry)
		case <-time.After(time.Second):
			a.Fail("timeout")
		}
	}

	a.Equal(len(expectedEntries), len(allEntries))
	for _, expected := range expectedEntries {
		a.Contains(allEntries, expected)
	}
}
Exemplo n.º 10
0
func assertChannelContains(a *assert.Assertions, entryC chan string, expectedEntries ...string) {
	var allEntries []string

WAITLOOP:
	for {
		select {
		case entry, ok := <-entryC:
			if !ok {
				break WAITLOOP
			}
			allEntries = append(allEntries, entry)
		case <-time.After(time.Second):
			a.Fail("timeout")
		}
	}

	a.Equal(len(expectedEntries), len(allEntries))
	for _, expected := range expectedEntries {
		a.Contains(allEntries, expected)
	}
}
Exemplo n.º 11
0
func runTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
	lexer := NewLexer()
	var err error
	_, err = lexer.tokenize(testcase.Expression)
	if err != nil {
		errMsg := fmt.Sprintf("(%s) Could not lex expression: %s -- %s", filename, testcase.Expression, err.Error())
		assert.Fail(errMsg)
		return
	}
	parser := NewParser()
	_, err = parser.Parse(testcase.Expression)
	if err != nil {
		errMsg := fmt.Sprintf("(%s) Could not parse expression: %s -- %s", filename, testcase.Expression, err.Error())
		assert.Fail(errMsg)
		return
	}
	actual, err := Search(testcase.Expression, given)
	if assert.Nil(err, fmt.Sprintf("Expression: %s", testcase.Expression)) {
		assert.Equal(testcase.Result, actual, fmt.Sprintf("Expression: %s", testcase.Expression))
	}
}
func messagePartitionReader(name string, a *assert.Assertions, store *MessagePartition, n int, done chan bool) {
	lastReadMessage := 0
	for lastReadMessage < n {

		msgC := make(chan MessageAndId)
		errorC := make(chan error)

		guble.Debug("[%v] start fetching at: %v", name, lastReadMessage+1)
		store.Fetch(FetchRequest{
			Partition:     "myMessages",
			StartId:       uint64(lastReadMessage + 1),
			Direction:     1,
			Count:         math.MaxInt32,
			MessageC:      msgC,
			ErrorCallback: errorC,
			StartCallback: make(chan int, 1),
		})

	fetch:

		for {
			select {
			case msgAndId, open := <-msgC:
				if !open {
					guble.Debug("[%v] stop fetching at %v", name, lastReadMessage)
					break fetch
				}
				a.Equal(lastReadMessage+1, int(msgAndId.Id))
				lastReadMessage = int(msgAndId.Id)
			case err := <-errorC:
				a.Fail("received error", err.Error())
				<-done
				return
			}
		}

	}
	guble.Debug("[%v] ready, got %v", name, lastReadMessage)
	done <- true
}
Exemplo n.º 13
0
func checkNode(t testing.TB, x *assert.Assertions, node Node) {
	acount, err := node.AdjacentCount()
	if err != nil {
		t.Fatal(err)
	}
	kcount, err := node.ChildCount()
	if err != nil {
		t.Fatal(err)
	}
	kids, err := node.Children()
	if err != nil {
		t.Fatal(err)
	}
	pcount, err := node.ParentCount()
	if err != nil {
		t.Fatal(err)
	}
	parents, err := node.Parents()
	if err != nil {
		t.Fatal(err)
	}
	if kcount != len(kids) {
		x.Fail("kcount != len(kids)")
	}
	if pcount != len(parents) {
		x.Fail("count != len(parents)")
	}
	if kcount+pcount != acount {
		x.Fail("kcount + pcount != acount")
	}
	for _, kid := range kids {
		checkKid(t, x, node, kid.(Node))
	}
	for _, parent := range parents {
		checkKid(t, x, parent.(Node), node)
	}
}
Exemplo n.º 14
0
func checkKid(t testing.TB, x *assert.Assertions, parent, kid Node) {
	pkids, err := parent.Children()
	if err != nil {
		t.Fatal(err)
	}
	found := false
	for _, pkid := range pkids {
		if bytes.Equal(pkid.Pattern().Label(), kid.Label()) {
			found = true
		}
	}
	if !found {
		t.Errorf("parent %v kids %v did not have %v", parent, pkids, kid)
		findChildren(parent, nil, true)
		t.Fatalf("assert-fail")
	}
	kparents, err := kid.Parents()
	if err != nil {
		t.Fatal(err)
	}
	found = false
	for _, kparent := range kparents {
		if bytes.Equal(kparent.Pattern().Label(), parent.Label()) {
			found = true
		}
	}
	if !found {
		t.Fatalf("kid %v parents %v did not have %v", kid, kparents, parent)
	}
	pkids, err = parent.CanonKids()
	if err != nil {
		errors.Logf("ERROR", "err = %v", err)
		x.Fail("err != nil")
		os.Exit(2)
	}
	found = false
	for _, pkid := range pkids {
		if bytes.Equal(pkid.Pattern().Label(), kid.Label()) {
			found = true
		}
	}
	if found {
		// kid is a canon kid
		// kid should have no other canon parents
		kparents, err := kid.Parents()
		if err != nil {
			t.Fatal(err)
		}
		found = false
		for _, kparent := range kparents {
			if bytes.Equal(kparent.Pattern().Label(), parent.Label()) {
				continue
			}
			kparent_ckids, err := kparent.CanonKids()
			if err != nil {
				t.Fatal(err)
			}
			for _, kparent_ckid := range kparent_ckids {
				if bytes.Equal(kparent_ckid.Pattern().Label(), kid.Label()) {
					x.Fail(errors.Errorf("kid %v had multiple canon parents %v %v", kid, parent, kparent).Error())
				}
			}
		}
	}
}