Skip to content
Snippets Groups Projects
Select Git revision
  • 735375b2dd2079722cad506ebecce5ecbec097f3
  • master default protected
  • v1.23.2
  • v1.23.1
  • v1.23.0
  • v1.22.0
  • v1.21.1
  • v1.21.0
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.4
  • v1.19.3
  • v1.19.2
  • v1.19.1
  • v1.19.0
  • v1.18.2
  • v1.18.1
  • v1.18.0
  • v1.17.0
  • v1.16.1
22 results

event-bus_test.go

Blame
  • event-bus_test.go 1.86 KiB
    package jobqueue
    
    import (
    	"sync"
    	"testing"
    	"time"
    )
    
    func TestSubscribeAndPublish(t *testing.T) {
    	eb := NewEventBus()
    
    	jobAddedCh := make(chan interface{}, 1)
    	eb.Subscribe(JobAdded, jobAddedCh)
    
    	jobData := "New Job Data"
    	eb.Publish(JobAdded, jobData)
    
    	select {
    	case receivedData := <-jobAddedCh:
    
    		rd := receivedData.(Event)
    
    		if rd.Data != jobData {
    			t.Errorf("Received data %v, want %v", receivedData, jobData)
    		}
    	case <-time.After(1 * time.Second):
    		t.Error("Timed out waiting for published event")
    	}
    }
    
    func TestUnsubscribe(t *testing.T) {
    	eb := NewEventBus()
    
    	jobAddedCh := make(chan interface{}, 1)
    	eb.Subscribe(JobAdded, jobAddedCh)
    	eb.Unsubscribe(JobAdded, jobAddedCh)
    
    	jobData := "New Job Data"
    	eb.Publish(JobAdded, jobData)
    
    	select {
    	case <-jobAddedCh:
    		t.Error("Received data after unsubscribing")
    	case <-time.After(1 * time.Second):
    		// Test passes if it times out (no data received)
    	}
    }
    
    func TestMultipleSubscribers(t *testing.T) {
    	eb := NewEventBus()
    
    	jobAddedCh1 := make(chan interface{}, 1)
    	jobAddedCh2 := make(chan interface{}, 1)
    	eb.Subscribe(JobAdded, jobAddedCh1)
    	eb.Subscribe(JobAdded, jobAddedCh2)
    
    	jobData := "New Job Data"
    	eb.Publish(JobAdded, jobData)
    
    	var wg sync.WaitGroup
    	wg.Add(2)
    
    	go func() {
    		defer wg.Done()
    		select {
    		case receivedData := <-jobAddedCh1:
    			rd := receivedData.(Event)
    			if rd.Data != jobData {
    				t.Errorf("Received data %v, want %v", receivedData, jobData)
    			}