e145d48f22
* feat: add plugin system with grpc runtime and native ui plugins - go plugin backends ship as release tarballs (per-platform binaries, checksums) that kandev installs from url/upload or filesystem sideload+sync, then spawns and supervises via hashicorp/go-plugin (unix socket / loopback tcp+automtls) - strict typed grpc contract (kandev.plugin.v1): DeliverEvent/InvokeTool/HandleWebhook plus a capability-gated host service (state, secrets, emit); author sdk in pkg/pluginsdk; packaging cli in cmd/plugin-pack - native frontend bundles: window.registerKandevPlugin + registry/host loader with dynamic routes, nav items, slots and ws handlers; plugins settings page with install dialog, sync button and enable/disable/uninstall - gated behind the plugins feature flag (prod off, dev/e2e on); covered by unit and integration tests plus playwright e2e against a real packaged fixture plugin Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): hydrate base app context on plugin routes Plugin-owned SPA routes classify as RouteUnknown, so the boot payload shipped only feature flags — no workspaces, workflows, or kanban context. Native plugin UI (e.g. host.ui.TaskCreateDialog) then had an empty store. Hydrate the same base context as the Home route for unknown SPA routes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(plugins): expose TaskCreateDialog on host.ui Lets a native plugin open kandev's real create-task modal (prefilled via initialValues) instead of POSTing directly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): harden filesystem/URL sinks against CodeQL findings Add sink-level guards on the plugin trust boundary: securejoin containment on archive extraction (zip-slip), plugin-id validation before every fs_store path, http/https scheme validation on install-from-URL, and non-interpolated ws-handler error logging. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): address review findings (races, resource caps, lifecycle) - serialize per-plugin startup to close a TOCTOU double-spawn race, and let a plugin restart after exhausting attempts - bound plugin startup with an explicit 30s timeout - reject backslash paths and cap decompressed bytes by actual size in pkgtar; only mark a package Signed when a verifier actually ran and passed - delete plugin_state on uninstall; validate webhook keys against the manifest and cap webhook body size; restart an active plugin when a new version installs Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): sanitize plugin-id path sinks for CodeQL Pin the plugin id to a single path segment with filepath.Base at recordPath/ configPath and pkgtar.Remove — a barrier the taint analysis recognizes. Behaviour-identical for valid ids (already validated by safePluginID); clears the go/path-injection alerts. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): inline plugin-id validation at path sinks for CodeQL filepath.Base does not strip '..', so it is not a path-injection barrier. Call the recognized safePluginID / id-pattern guard inline in Delete/GetConfig/ SetConfig and pkgtar.Remove — the same barrier that already cleared the Get sink. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): address code-review findings (backend) - add capability-gating tests for the Host RPC service (was untested) - close a stop-during-start orphan race so Disable/Uninstall can't leave a supervised subprocess behind - on failed upgrade, remove only the new version dir (not all versions + data); restart the prior version - validate manifest version and reject ids ending in .config (record-file alias) - make Deliverer.Refresh non-blocking and cancel in-flight delivery on worker stop - clamp out-of-range webhook status, skip-and-warn corrupt records in List, write records atomically, semver-aware sideload version pick Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): address code-review findings (frontend) - wrap plugin routes + settings routes in a shared PluginErrorBoundary so a throwing plugin page shows a fallback instead of white-screening the SPA - fix disable/re-enable in one session: keep the cached registration and re-run initialize instead of relying on a cached ES-module re-import - prefix root-relative bundle/style URLs with apiBaseUrl (split-origin/Tauri) - surface partial-install warnings via toast.warning instead of a bare success - dedupe/remove injected style links on unload; gate PluginNavItems on the flag Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(plugins): serialize per-plugin lifecycle + guard config writes - add a per-plugin keyed lock in Service so concurrent Enable/Disable/Install/ Uninstall/UpdateConfig for one id are serialized (no status-machine race) - take s.mu in SetConfig/GetConfig and write config atomically - bound each plugin initialize() with a timeout so one hung bundle can't stall the rest of the boot load Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(plugins): split service_test.go to satisfy file-length lint Move the install/URL test group into service_install_test.go; no logic change. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * ci: build and ship the e2e fixture plugin package to E2E shards The plugin E2E specs' global-setup requires apps/backend/.build/kandev-plugin-e2e-1.0.0.tar.gz. Build it in the E2E build job, upload it as an artifact, and download it in both the regular and container shard jobs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
444 lines
15 KiB
Go
444 lines
15 KiB
Go
package delivery
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kandev/kandev/internal/common/logger"
|
|
"github.com/kandev/kandev/internal/events/bus"
|
|
"github.com/kandev/kandev/internal/plugins/store"
|
|
"github.com/kandev/kandev/pkg/pluginsdk"
|
|
)
|
|
|
|
// fakeTransport is a controllable Transport for tests: DeliverEvent calls
|
|
// through to an injectable handler (defaulting to a no-op success), so
|
|
// tests can assert on what was delivered and simulate failures/blocking
|
|
// without a real network round trip.
|
|
type fakeTransport struct {
|
|
mu sync.Mutex
|
|
handler func(pluginID string, e *pluginsdk.Event) error
|
|
// ctxHandler, when set, takes priority over handler and additionally
|
|
// receives DeliverEvent's ctx — for tests that need to observe/react to
|
|
// ctx cancellation (e.g. proving an in-flight attempt is interrupted by
|
|
// worker stop()).
|
|
ctxHandler func(ctx context.Context, pluginID string, e *pluginsdk.Event) error
|
|
}
|
|
|
|
func (f *fakeTransport) DeliverEvent(ctx context.Context, pluginID string, e *pluginsdk.Event) error {
|
|
f.mu.Lock()
|
|
ch := f.ctxHandler
|
|
h := f.handler
|
|
f.mu.Unlock()
|
|
if ch != nil {
|
|
return ch(ctx, pluginID, e)
|
|
}
|
|
if h == nil {
|
|
return nil
|
|
}
|
|
return h(pluginID, e)
|
|
}
|
|
|
|
func (f *fakeTransport) setHandler(h func(pluginID string, e *pluginsdk.Event) error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.handler = h
|
|
}
|
|
|
|
func (f *fakeTransport) setCtxHandler(h func(ctx context.Context, pluginID string, e *pluginsdk.Event) error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.ctxHandler = h
|
|
}
|
|
|
|
// fakeLister is a mutable, concurrency-safe PluginLister for tests.
|
|
type fakeLister struct {
|
|
mu sync.Mutex
|
|
records []PluginRecord
|
|
}
|
|
|
|
func (f *fakeLister) ActivePlugins() []PluginRecord {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
out := make([]PluginRecord, len(f.records))
|
|
copy(out, f.records)
|
|
return out
|
|
}
|
|
|
|
func (f *fakeLister) set(records ...PluginRecord) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.records = records
|
|
}
|
|
|
|
// newTestDeliverer wires a Deliverer with zero retry backoff by default so
|
|
// tests never rely on real sleeps; individual tests override retry delays
|
|
// when they specifically exercise backoff/retry behavior.
|
|
func newTestDeliverer(t *testing.T, eventBus bus.EventBus, transport Transport, lister PluginLister, opts ...Option) *Deliverer {
|
|
t.Helper()
|
|
base := []Option{WithRetryDelays(nil)}
|
|
d := New(eventBus, transport, lister, logger.Default(), append(base, opts...)...)
|
|
t.Cleanup(d.Stop)
|
|
return d
|
|
}
|
|
|
|
func requireNoTimeout[T any](t *testing.T, ch <-chan T, timeout time.Duration, what string) T {
|
|
t.Helper()
|
|
select {
|
|
case v := <-ch:
|
|
return v
|
|
case <-time.After(timeout):
|
|
t.Fatalf("timed out waiting for %s", what)
|
|
var zero T
|
|
return zero
|
|
}
|
|
}
|
|
|
|
func requireTimeout[T any](t *testing.T, ch <-chan T, window time.Duration, what string) {
|
|
t.Helper()
|
|
select {
|
|
case <-ch:
|
|
t.Fatalf("unexpected %s", what)
|
|
case <-time.After(window):
|
|
}
|
|
}
|
|
|
|
func TestDeliverer_DeliversOnMatchingEvent(t *testing.T) {
|
|
receivedCh := make(chan *pluginsdk.Event, 1)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(pluginID string, e *pluginsdk.Event) error {
|
|
if pluginID != "plug1" {
|
|
t.Errorf("DeliverEvent pluginID = %q, want plug1", pluginID)
|
|
}
|
|
receivedCh <- e
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister)
|
|
d.Refresh()
|
|
|
|
ev := bus.NewEvent("task.state_changed", "test", map[string]interface{}{"task_id": "abc"})
|
|
if err := eventBus.Publish(context.Background(), "task.state_changed", ev); err != nil {
|
|
t.Fatalf("Publish: %v", err)
|
|
}
|
|
|
|
got := requireNoTimeout(t, receivedCh, 2*time.Second, "event delivery")
|
|
if got.EventType != "task.state_changed" {
|
|
t.Errorf("EventType = %q, want task.state_changed", got.EventType)
|
|
}
|
|
if got.EventID == "" {
|
|
t.Error("EventID must not be empty")
|
|
}
|
|
if got.Payload["task_id"] != "abc" {
|
|
t.Errorf("Payload[task_id] = %v, want abc", got.Payload["task_id"])
|
|
}
|
|
}
|
|
|
|
func TestDeliverer_NonMatchingEventIsNotDelivered(t *testing.T) {
|
|
receivedCh := make(chan struct{}, 1)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(string, *pluginsdk.Event) error {
|
|
receivedCh <- struct{}{}
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister)
|
|
d.Refresh()
|
|
|
|
ev := bus.NewEvent("office.comment.created", "test", map[string]interface{}{})
|
|
if err := eventBus.Publish(context.Background(), "office.comment.created", ev); err != nil {
|
|
t.Fatalf("Publish: %v", err)
|
|
}
|
|
|
|
requireTimeout(t, receivedCh, 150*time.Millisecond, "delivery for non-subscribed subject")
|
|
}
|
|
|
|
func TestDeliverer_SequentialPerPlugin(t *testing.T) {
|
|
var inFlight int32
|
|
release := make(chan struct{})
|
|
arrivedCh := make(chan string, 10)
|
|
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(_ string, e *pluginsdk.Event) error {
|
|
if atomic.AddInt32(&inFlight, 1) > 1 {
|
|
t.Errorf("more than one concurrent delivery in flight for the same plugin")
|
|
}
|
|
arrivedCh <- e.EventID
|
|
<-release
|
|
atomic.AddInt32(&inFlight, -1)
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister)
|
|
d.Refresh()
|
|
|
|
ctx := context.Background()
|
|
_ = eventBus.Publish(ctx, "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
_ = eventBus.Publish(ctx, "task.updated", bus.NewEvent("task.updated", "test", map[string]interface{}{}))
|
|
|
|
first := requireNoTimeout(t, arrivedCh, 2*time.Second, "first delivery")
|
|
|
|
// Second delivery must not start while the first is still in flight.
|
|
requireTimeout(t, arrivedCh, 150*time.Millisecond, "second delivery starting before first completed")
|
|
|
|
release <- struct{}{}
|
|
second := requireNoTimeout(t, arrivedCh, 2*time.Second, "second delivery")
|
|
release <- struct{}{}
|
|
|
|
if first == second {
|
|
t.Errorf("expected two distinct delivery ids, got the same twice: %q", first)
|
|
}
|
|
}
|
|
|
|
func TestDeliverer_RetriesOnErrorThenSucceeds(t *testing.T) {
|
|
var attempts int32
|
|
arrivedCh := make(chan struct{}, 5)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(string, *pluginsdk.Event) error {
|
|
n := atomic.AddInt32(&attempts, 1)
|
|
arrivedCh <- struct{}{}
|
|
if n < 3 {
|
|
return fmt.Errorf("simulated failure %d", n)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister,
|
|
WithRetryDelays([]time.Duration{0, 0, 0}))
|
|
d.Refresh()
|
|
|
|
_ = eventBus.Publish(context.Background(), "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
|
|
for i := 0; i < 3; i++ {
|
|
requireNoTimeout(t, arrivedCh, 2*time.Second, fmt.Sprintf("attempt %d", i+1))
|
|
}
|
|
if got := atomic.LoadInt32(&attempts); got != 3 {
|
|
t.Errorf("attempts = %d, want 3 (2 failures + 1 success)", got)
|
|
}
|
|
}
|
|
|
|
func TestDeliverer_GivesUpAfterAllRetries(t *testing.T) {
|
|
var attempts int32
|
|
arrivedCh := make(chan struct{}, 10)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(string, *pluginsdk.Event) error {
|
|
atomic.AddInt32(&attempts, 1)
|
|
arrivedCh <- struct{}{}
|
|
return fmt.Errorf("simulated permanent failure")
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister,
|
|
WithRetryDelays([]time.Duration{0, 0, 0}))
|
|
d.Refresh()
|
|
|
|
_ = eventBus.Publish(context.Background(), "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
|
|
for i := 0; i < 4; i++ {
|
|
requireNoTimeout(t, arrivedCh, 2*time.Second, fmt.Sprintf("attempt %d", i+1))
|
|
}
|
|
requireTimeout(t, arrivedCh, 150*time.Millisecond, "a 5th attempt (should give up after 1+3 retries)")
|
|
if got := atomic.LoadInt32(&attempts); got != 4 {
|
|
t.Errorf("attempts = %d, want 4 (1 initial + 3 retries)", got)
|
|
}
|
|
}
|
|
|
|
func TestDeliverer_BuffersWhileErrorAndFlushReplaysInOrder(t *testing.T) {
|
|
arrivedCh := make(chan string, 10)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(_ string, e *pluginsdk.Event) error {
|
|
arrivedCh <- e.EventType
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusError})
|
|
|
|
processedCh := make(chan struct{}, 10)
|
|
d := newTestDeliverer(t, eventBus, transport, lister,
|
|
withOnProcessed(func(string, Delivery) { processedCh <- struct{}{} }))
|
|
d.Refresh()
|
|
|
|
ctx := context.Background()
|
|
_ = eventBus.Publish(ctx, "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
_ = eventBus.Publish(ctx, "task.updated", bus.NewEvent("task.updated", "test", map[string]interface{}{}))
|
|
|
|
// Wait until the worker has actually buffered both events before
|
|
// flipping status and flushing, so this test doesn't race the worker
|
|
// goroutine's queue-drain against the recovery transition below.
|
|
requireNoTimeout(t, processedCh, 2*time.Second, "first event buffered")
|
|
requireNoTimeout(t, processedCh, 2*time.Second, "second event buffered")
|
|
|
|
requireTimeout(t, arrivedCh, 150*time.Millisecond, "delivery while plugin is in error state")
|
|
|
|
// Recovery: status flips to active (mirrors Service.handleStatusChange
|
|
// after the runtime manager reports the plugin healthy again) and
|
|
// Refresh/Flush are called, mirroring the
|
|
// Service.SetStatus -> Refresh, Service.handleStatusChange -> Flush
|
|
// contract.
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
d.Refresh()
|
|
d.Flush("plug1")
|
|
|
|
first := requireNoTimeout(t, arrivedCh, 2*time.Second, "replayed first buffered event")
|
|
second := requireNoTimeout(t, arrivedCh, 2*time.Second, "replayed second buffered event")
|
|
|
|
if first != "task.created" || second != "task.updated" {
|
|
t.Errorf("replay order = [%s %s], want [task.created task.updated]", first, second)
|
|
}
|
|
}
|
|
|
|
func TestDeliverer_RingBufferOverflowDropsOldest(t *testing.T) {
|
|
arrivedCh := make(chan string, 10)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(_ string, e *pluginsdk.Event) error {
|
|
arrivedCh <- e.EventType
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusError})
|
|
|
|
processedCh := make(chan struct{}, 10)
|
|
d := newTestDeliverer(t, eventBus, transport, lister, WithRingBuffer(2, 5*time.Minute),
|
|
withOnProcessed(func(string, Delivery) { processedCh <- struct{}{} }))
|
|
d.Refresh()
|
|
|
|
ctx := context.Background()
|
|
_ = eventBus.Publish(ctx, "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
_ = eventBus.Publish(ctx, "task.updated", bus.NewEvent("task.updated", "test", map[string]interface{}{}))
|
|
_ = eventBus.Publish(ctx, "task.deleted", bus.NewEvent("task.deleted", "test", map[string]interface{}{}))
|
|
|
|
// Wait until the worker has buffered all three events (and evicted the
|
|
// oldest) before flipping status and flushing.
|
|
for i := 0; i < 3; i++ {
|
|
requireNoTimeout(t, processedCh, 2*time.Second, fmt.Sprintf("event %d buffered", i+1))
|
|
}
|
|
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
d.Refresh()
|
|
d.Flush("plug1")
|
|
|
|
first := requireNoTimeout(t, arrivedCh, 2*time.Second, "replayed first surviving buffered event")
|
|
second := requireNoTimeout(t, arrivedCh, 2*time.Second, "replayed second surviving buffered event")
|
|
if first != "task.updated" || second != "task.deleted" {
|
|
t.Errorf("replay order = [%s %s], want [task.updated task.deleted] (task.created should have been dropped as oldest on overflow)", first, second)
|
|
}
|
|
requireTimeout(t, arrivedCh, 150*time.Millisecond, "a 3rd replayed event")
|
|
}
|
|
|
|
func TestDeliverer_RefreshStopsDeliveryWhenPluginRemoved(t *testing.T) {
|
|
arrivedCh := make(chan struct{}, 5)
|
|
transport := &fakeTransport{}
|
|
transport.setHandler(func(string, *pluginsdk.Event) error {
|
|
arrivedCh <- struct{}{}
|
|
return nil
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister)
|
|
d.Refresh()
|
|
|
|
_ = eventBus.Publish(context.Background(), "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
requireNoTimeout(t, arrivedCh, 2*time.Second, "delivery while plugin active")
|
|
|
|
// Disabled: PluginLister stops returning it (mirrors Service filtering
|
|
// out StatusDisabled).
|
|
lister.set()
|
|
d.Refresh()
|
|
|
|
_ = eventBus.Publish(context.Background(), "task.updated", bus.NewEvent("task.updated", "test", map[string]interface{}{}))
|
|
requireTimeout(t, arrivedCh, 150*time.Millisecond, "delivery after plugin removed from active set")
|
|
}
|
|
|
|
// TestDeliverer_RefreshReturnsPromptlyDuringInFlightDelivery pins the fix
|
|
// for Refresh blocking on a worker's in-flight delivery attempt: before the
|
|
// fix, attemptDeliver's ctx was context.Background() (only bounded by
|
|
// requestTimeout, here deliberately set far longer than the test's
|
|
// timeout), and stop() waited on that same attempt to finish before
|
|
// returning — so Refresh (which calls stop() for a removed worker) could
|
|
// block for the full requestTimeout. Service.handleStatusChange calls
|
|
// Refresh from the runtime supervision goroutine, which must never block
|
|
// (runtime.NewManager's contract). The fix derives attemptDeliver's ctx
|
|
// from a per-worker context canceled by stop(), so the in-flight attempt
|
|
// is interrupted immediately instead.
|
|
func TestDeliverer_RefreshReturnsPromptlyDuringInFlightDelivery(t *testing.T) {
|
|
entered := make(chan struct{})
|
|
released := make(chan struct{})
|
|
var sawCancel int32
|
|
|
|
transport := &fakeTransport{}
|
|
transport.setCtxHandler(func(ctx context.Context, _ string, _ *pluginsdk.Event) error {
|
|
close(entered)
|
|
select {
|
|
case <-ctx.Done():
|
|
atomic.AddInt32(&sawCancel, 1)
|
|
return ctx.Err()
|
|
case <-released:
|
|
return nil
|
|
}
|
|
})
|
|
|
|
eventBus := bus.NewMemoryEventBus(logger.Default())
|
|
lister := &fakeLister{}
|
|
lister.set(PluginRecord{ID: "plug1", EventSubjects: []string{"task.*"}, Status: store.StatusActive})
|
|
|
|
d := newTestDeliverer(t, eventBus, transport, lister, WithRequestTimeout(30*time.Second))
|
|
d.Refresh()
|
|
|
|
_ = eventBus.Publish(context.Background(), "task.created", bus.NewEvent("task.created", "test", map[string]interface{}{}))
|
|
select {
|
|
case <-entered:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("timed out waiting for the delivery attempt to start")
|
|
}
|
|
|
|
// Remove the plugin so Refresh must tear its worker down while the
|
|
// delivery attempt above is still blocked inside the transport call.
|
|
lister.set()
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
d.Refresh()
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("Refresh() did not return promptly while a delivery was in flight (requestTimeout was 30s)")
|
|
}
|
|
|
|
if atomic.LoadInt32(&sawCancel) != 1 {
|
|
t.Fatal("in-flight delivery attempt was never canceled by worker stop()")
|
|
}
|
|
close(released)
|
|
}
|