Skip to content
Snippets Groups Projects
events_test.go 2 KiB
Newer Older
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

	"github.com/hyperledger/burrow/txs"
Silas Davis's avatar
Silas Davis committed
	"github.com/stretchr/testify/assert"
)

func TestMultiplexedEvents(t *testing.T) {
	emitter1 := newMockEventEmitter()
	emitter2 := newMockEventEmitter()
	emitter12 := Multiplex(emitter1, emitter2)

	eventData1 := make(map[txs.EventData]int)
	eventData2 := make(map[txs.EventData]int)
	eventData12 := make(map[txs.EventData]int)

	mutex1 := &sync.Mutex{}
	mutex2 := &sync.Mutex{}
	mutex12 := &sync.Mutex{}

	emitter12.Subscribe("Sub12", "Event12", func(eventData txs.EventData) {
		mutex12.Lock()
		eventData12[eventData] = 1
		mutex12.Unlock()
	})
	emitter1.Subscribe("Sub1", "Event1", func(eventData txs.EventData) {
		mutex1.Lock()
		eventData1[eventData] = 1
		mutex1.Unlock()
	})
	emitter2.Subscribe("Sub2", "Event2", func(eventData txs.EventData) {
		mutex2.Lock()
		eventData2[eventData] = 1
		mutex2.Unlock()
	})

	time.Sleep(mockInterval)

	allEventData := make(map[txs.EventData]int)
	for k, v := range eventData1 {
		allEventData[k] = v
	}
	for k, v := range eventData2 {
		allEventData[k] = v
	}

	assert.Equal(t, map[txs.EventData]int{mockEventData{"Sub1", "Event1"}: 1},
	assert.Equal(t, map[txs.EventData]int{mockEventData{"Sub2", "Event2"}: 1},
	assert.Equal(t, map[txs.EventData]int{mockEventData{"Sub12", "Event12"}: 1},
		eventData12)

	assert.NotEmpty(t, allEventData, "Some events should have been published")
}