Files
bj_power/bj_power_mes/internal/processor/integration_test.go
T

75 lines
1.7 KiB
Go

package processor
import (
"context"
"sync"
"testing"
"time"
"bj_power_mes/internal/eventbus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEventBus_JobStatusChanged(t *testing.T) {
bus := eventbus.NewLocalBus()
ctx := context.Background()
var mu sync.Mutex
var received []eventbus.Event
bus.Subscribe(eventbus.EventLoad, func(ctx context.Context, e eventbus.Event) error {
mu.Lock()
received = append(received, e)
mu.Unlock()
return nil
})
evt := eventbus.Event{Type: eventbus.EventLoad, EntityType: "job", EntityID: "1", Payload: map[string]any{"jobId": 1}}
err := bus.Publish(ctx, evt)
require.NoError(t, err)
assert.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(received) >= 1
}, 2*time.Second, 10*time.Millisecond)
mu.Lock()
defer mu.Unlock()
assert.Equal(t, eventbus.EventLoad, received[0].Type)
assert.Equal(t, "job", received[0].EntityType)
assert.Equal(t, "1", received[0].EntityID)
}
func TestEventBus_TempSlotChanged(t *testing.T) {
bus := eventbus.NewLocalBus()
ctx := context.Background()
var mu sync.Mutex
var received []eventbus.Event
bus.Subscribe(eventbus.EventType("TEMP_SLOT_CHANGED"), func(ctx context.Context, e eventbus.Event) error {
mu.Lock()
received = append(received, e)
mu.Unlock()
return nil
})
evt := eventbus.NewTempSlotChangedEvent(3, 100, "allocate")
err := bus.Publish(ctx, evt)
require.NoError(t, err)
assert.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(received) >= 1
}, 2*time.Second, 10*time.Millisecond)
mu.Lock()
defer mu.Unlock()
assert.Equal(t, "temp_slot", received[0].EntityType)
assert.Equal(t, "3", received[0].EntityID)
}