Skip to content
Snippets Groups Projects
Select Git revision
  • ec0db468567b15296e0ee858959b83cfd38d5086
  • master default protected
  • 1.31
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
23 results

translations.js

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)
    			}
    		case <-time.After(1 * time.Second):
    			t.Error("Timed out waiting for published event on channel 1")
    		}
    	}()
    
    	go func() {
    		defer wg.Done()
    		select {
    		case receivedData := <-jobAddedCh2:
    			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 on channel 2")
    		}
    	}()
    
    	wg.Wait()
    }