예제 #1
0
func TestHandleAbPublish(t *testing.T) {
	for i, test := range handleAbPublishTests {

		// Manually add subscriber channels and topics
		for j := 0; j < test.numSubscribers; j++ {
			test.broker.AddSubscriber(uint64(j), []byte("12345"))
			test.broker.addToSubChannel(uint64(j))
			test.broker.subscribers[uint64(j)].topics[1] = true
			test.broker.subscribers[uint64(j)].topics[2] = true
			test.broker.subscribers[uint64(j)].topics[3] = true
		}

		for j, subtest := range test.subtests {
			// Add publication request
			test.broker.handleAbPublish(&subtest.pubReq)

			// Check that all "subscribers" got the forwarded publication
			test.broker.subscribersMutex.RLock()
			for _, subscriber := range test.broker.subscribers {
				select {
				case pub := <-subscriber.toCh:
					if !common.Equals(pub, subtest.want) {
						t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
							i+1, test.desc, j+1, &subtest.want, pub)
					}
				}
			}
			test.broker.subscribersMutex.RUnlock()
		}
	}
}
예제 #2
0
func TestHandleBrbPublish(t *testing.T) {
	for i, test := range handleBrbPublishTests {

		// Manually add other broker channels
		for j := 1; j < test.numBrokers; j++ {
			test.broker.addBrokerChannels(uint64(j))
		}

		for j, subtest := range test.subtests {
			// Add publication request
			test.broker.handleBrbPublish(&subtest.pubReq)

			// Check that all other "brokers" got the echoed publication
			test.broker.remoteBrokersMutex.RLock()
			for _, remoteBroker := range test.broker.remoteBrokers {
				select {
				case pub := <-remoteBroker.toEchoCh:
					if !common.Equals(pub, subtest.want) {
						t.Errorf("HandleBrbPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
							i+1, test.desc, j+1, &subtest.want, pub)
					}
				}
			}
			test.broker.remoteBrokersMutex.RUnlock()
		}
	}
}
func TestPublish(t *testing.T) {
	for i, test := range publishTests {

		// Manually add broker channels
		for j := 0; j < test.numBrokers; j++ {
			test.publisher.AddBroker(uint64(j), "", []byte("12345"))
			test.publisher.addChannel(uint64(j))
		}

		for j, subtest := range test.subtests {
			// Not actually going to get responses from publishers, so just preload the accept channel
			for k := 0; k < test.numBrokers; k++ {
				test.publisher.statusCh <- pb.PubResponse_OK
			}

			// Add publication request
			test.publisher.Publish(&subtest.pub)

			// Check that all "brokers" got the publication
			for _, broker := range test.publisher.brokers {
				select {
				case pub := <-broker.toCh:
					if !common.Equals(pub, subtest.want) {
						t.Errorf("TestPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
							i+1, test.desc, j+1, subtest.want, pub)
					}
				}
			}
		}
	}
}
func TestHandleHistory(t *testing.T) {
	for i, test := range handleHistoryTests {
		go test.subscriber.handlePublications()

		for j, subtest := range test.subtests {
			// Add publication
			select {
			case test.subscriber.fromBrokerCh <- subtest.pubReq:
			}

			var got []pb.Publication

			for k := 0; k < subtest.output; k++ {
				select {
				case pub := <-test.subscriber.ToUserPubCh:
					got = append(got, pub)
				}
			}

			if len(got) != len(subtest.want) {
				t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
					i+1, test.desc, j+1, subtest.want, got)
				continue
			}

			for _, wantPub := range subtest.want {
				foundMatch := false
				for _, gotPub := range got {
					foundMatch = common.Equals(wantPub, gotPub)

					if foundMatch {
						break // for _, gotPub := range got
					}
				}

				if !foundMatch {
					t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot:  %v\n",
						i+1, test.desc, j+1, subtest.want, got)
					break // for _, wantPub := range subtest.want
				}
			}
		}
	}
}
func TestHandlePublications(t *testing.T) {
	for i, test := range handlePublishTests {
		go test.subscriber.handlePublications()

		for j, subtest := range test.subtests {
			// Add publication
			select {
			case test.subscriber.fromBrokerCh <- subtest.pubReq:
			}

			if subtest.output {
				select {
				case pub := <-test.subscriber.ToUserPubCh:
					if !common.Equals(pub, subtest.want) {
						t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
							i+1, test.desc, j+1, &subtest.want, pub)
					}
				}
			}
		}
	}
}
func TestHistory(t *testing.T) {
	for i, test := range historyTests {

		// Manually add broker channels
		for j := 0; j < test.numBrokers; j++ {
			test.publisher.AddBroker(uint64(j), "", []byte("12345"))
			test.publisher.addChannel(uint64(j))
		}

		for j, subtest := range test.subtests {
			timeForHistory := false
			if j == len(test.subtests)-1 {
				timeForHistory = true
			}

			// Not actually going to get responses from publishers, so just preload the status channel
			if timeForHistory {
				for k := 0; k < test.numBrokers; k++ {
					test.publisher.statusCh <- pb.PubResponse_HISTORY
				}

				// Preload the status channel with history responses when the channel is available
				go func() {
					for k := 0; k < test.numBrokers; k++ {
						test.publisher.statusCh <- pb.PubResponse_OK
					}
				}()

			} else {
				for k := 0; k < test.numBrokers; k++ {
					test.publisher.statusCh <- pb.PubResponse_OK
				}
			}

			// Add publication request
			test.publisher.Publish(&subtest.pub)

			// Check that all "brokers" got the publication
			test.publisher.brokersMutex.RLock()
			for _, broker := range test.publisher.brokers {
				select {
				case pub := <-broker.toCh:
					if !common.Equals(pub, subtest.want) {
						t.Errorf("TestPublish\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
							i+1, test.desc, j+1, subtest.want, pub)
					}
				}
			}
			test.publisher.brokersMutex.RUnlock()
		}

		test.publisher.brokersMutex.RLock()
		for _, broker := range test.publisher.brokers {
			select {
			case pub := <-broker.toCh:
				if !common.Equals(pub, test.wantHistory) {
					t.Errorf("TestPublish\ntest nr:%d\ndescription: %s\nwant: %v\ngot: %v\n",
						i+1, test.desc, test.wantHistory, pub)
				}
			}
		}
		test.publisher.brokersMutex.RUnlock()
	}
}
예제 #7
0
func TestHandleChainPublish(t *testing.T) {
	for i, test := range handleChainPublishTests {
		chain := make(map[string][]string)
		rChain := make(map[string][]string)

		// Add descendants
		for _, descendantStr := range test.descendants {
			temp := strings.Split(descendantStr, ":")
			node := temp[0]

			// If there are children to the node
			if len(temp) == 2 {
				children := strings.Split(temp[1], ",")
				if len(children) != 0 && children[0] != "" {
					chain[node] = children
				} else {
					chain[node] = nil
				}
			} else {
				chain[node] = nil
			}

			if node == test.thisNode {

			} else if strings.HasPrefix(node, "S") {
				id, err := strconv.ParseUint(node[1:], 10, 64)
				if err != nil {
					fmt.Printf("Error parsing %v.\n", node)
					continue
				}
				test.broker.AddSubscriber(id, []byte(""))
				test.broker.addToSubChannel(id)
				test.broker.subscribers[id].topics[1] = true
				test.broker.subscribers[id].topics[2] = true
				test.broker.subscribers[id].topics[3] = true
			} else if strings.HasPrefix(node, "B") {
				id, err := strconv.ParseUint(node[1:], 10, 64)
				if err != nil {
					fmt.Printf("Error parsing %v.\n", node)
					continue
				}
				test.broker.AddBroker(id, "", []byte(""))
				test.broker.addBrokerChannels(id)
			}
		}

		// Add ancestors
		for _, ancestorStr := range test.ancestors {
			temp := strings.Split(ancestorStr, ":")
			node := temp[0]

			// If there are parents to the node
			if len(temp) == 2 {
				parents := strings.Split(temp[1], ",")
				if len(parents) != 0 && parents[0] != "" {
					rChain[node] = parents
				} else {
					rChain[node] = nil
				}
			} else {
				rChain[node] = nil
			}

			if node == test.thisNode {

			} else if strings.HasPrefix(node, "P") {
				id, err := strconv.ParseUint(node[1:], 10, 64)
				if err != nil {
					fmt.Printf("Error parsing %v.\n", node)
					continue
				}
				test.broker.AddPublisher(id, []byte(""))
			} else if strings.HasPrefix(node, "B") {
				id, err := strconv.ParseUint(node[1:], 10, 64)
				if err != nil {
					fmt.Printf("Error parsing %v.\n", node)
					continue
				}
				test.broker.AddBroker(id, "", []byte(""))
				test.broker.addBrokerChannels(id)
			}
		}

		test.broker.chainRange = test.chainRange

		test.broker.AddChainPath(chain, rChain)

		// Add publication request
		sent := test.broker.handleChainPublish(&test.pubReq)
		if sent == false && test.wantPubs == true {
			t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\nwant: %v\ngot: %v\n",
				i+1, test.desc, &test.want, "Publication not sent.")
			continue
		}

		// Check that all "subscribers" got the forwarded publication
		for _, childStr := range test.broker.chainNodes[test.thisNode].children {

			id, err := strconv.ParseUint(childStr[1:], 10, 64)
			if err != nil {
				fmt.Printf("Error parsing %v.\n", childStr)
				continue
			}

			if strings.HasPrefix(childStr, "B") {
				broker := test.broker.remoteBrokers[id]
				select {
				case pub := <-broker.toChainCh:
					// Currently this is not checking each individual Chain MAC, only the length of ChainMACs slice.
					if !common.Equals(pub, test.want) {
						t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\nwant: %v\ngot: %v\n",
							i+1, test.desc, &test.want, &pub)
					}
				}
			} else if strings.HasPrefix(childStr, "S") {
				subscriber := test.broker.subscribers[id]
				select {
				case pub := <-subscriber.toCh:
					// Currently this is not checking each individual Chain MAC, only the length of ChainMACs slice.
					if !common.Equals(pub, test.want) {
						t.Errorf("HandleAbPublish\ntest nr:%d\ndescription: %s\nwant: %v\ngot: %v\n",
							i+1, test.desc, &test.want, &pub)
					}
				}
			}
		}
	}
}
예제 #8
0
func TestHandleBrbReady(t *testing.T) {
	for i, test := range handleBrbReadyTests {

		// Manually add other broker channels
		for j := 1; j < test.numBrokers; j++ {
			test.broker.addBrokerChannels(uint64(j))
		}

		// Manually add subscriber channels
		for j := 0; j < test.numSubscribers; j++ {
			test.broker.AddSubscriber(uint64(j), []byte("12345"))
			test.broker.addToSubChannel(uint64(j))
			test.broker.subscribers[uint64(j)].topics[1] = true
			test.broker.subscribers[uint64(j)].topics[2] = true
			test.broker.subscribers[uint64(j)].topics[3] = true
		}

		// Manually add the publications already readied
		for _, pub := range test.alreadyReadied {
			if test.broker.readiesSent[pub.PublisherID] == nil {
				test.broker.readiesSent[pub.PublisherID] = make(map[int64]bool)
			}
			test.broker.readiesSent[pub.PublisherID][pub.PublicationID] = true
		}

		for j, subtest := range test.subtests {
			// Add ready request
			test.broker.handleReady(&subtest.ready)

			if subtest.output == true {
				// Check that all other "brokers" got the readied publication
				test.broker.remoteBrokersMutex.RLock()
				for _, remoteBroker := range test.broker.remoteBrokers {
					if len(remoteBroker.toReadyCh) != 1 {
						t.Errorf("HandleBrbReady\ntest nr:%d\ndescription: %s\naction nr: %d\nBroker channel should have 1.\nThere are %v publications.\n",
							i+1, test.desc, j+1, len(remoteBroker.toReadyCh))
						continue
					}
					select {
					case pub := <-remoteBroker.toReadyCh:
						if !common.Equals(pub, subtest.want) {
							t.Errorf("HandleBrbReady\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
								i+1, test.desc, j+1, &subtest.want, pub)
						}
					}
				}
				test.broker.remoteBrokersMutex.RUnlock()

				// Check that all "subscribers" got the readied publication
				test.broker.subscribersMutex.RLock()
				for _, subscriber := range test.broker.subscribers {
					if len(subscriber.toCh) != 1 {
						t.Errorf("HandleBrbReady\ntest nr:%d\ndescription: %s\naction nr: %d\nSub channel should have 1.\nThere are %v publications.\n",
							i+1, test.desc, j+1, len(subscriber.toCh))
						continue
					}
					select {
					case pub := <-subscriber.toCh:
						if !common.Equals(pub, subtest.want) {
							t.Errorf("HandleBrbReady\ntest nr:%d\ndescription: %s\naction nr: %d\nwant: %v\ngot: %v\n",
								i+1, test.desc, j+1, &subtest.want, pub)
						}
					}
				}
				test.broker.subscribersMutex.RUnlock()
			} else {
				// Check that all other "brokers" have empty channels
				test.broker.remoteBrokersMutex.RLock()
				for _, remoteBroker := range test.broker.remoteBrokers {
					if len(remoteBroker.toReadyCh) > 0 {
						t.Errorf("HandleBrbReady\ntest nr:%d\ndescription: %s\naction nr: %d\nBroker channel should be empty.\nThere is %v publication(s).\n",
							i+1, test.desc, j+1, len(remoteBroker.toReadyCh))
					}
				}
				test.broker.remoteBrokersMutex.RUnlock()

				// Check that all "subscribers" have empty channels
				test.broker.subscribersMutex.RLock()
				for _, subscriber := range test.broker.subscribers {
					if len(subscriber.toCh) > 0 {
						t.Errorf("HandleBrbReady\ntest nr:%d\ndescription: %s\naction nr: %d\nSub channel should be empty.\nThere is %v publication(s).\n",
							i+1, test.desc, j+1, len(subscriber.toCh))
					}
				}
				test.broker.subscribersMutex.RUnlock()
			}
		}
	}
}