Skip to content

Commit

Permalink
Add monitoring in time event testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Gabitov committed Dec 10, 2024
1 parent 162888b commit ba3fcdc
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions internal/eventproc/eventhub/waiters/waiters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package waiters_test

import (
"context"
"log/slog"
"os"
"testing"
"time"

Expand All @@ -14,6 +16,8 @@ import (
"github.com/dr-dobermann/gobpm/pkg/model/events"
"github.com/dr-dobermann/gobpm/pkg/model/flow"
"github.com/dr-dobermann/gobpm/pkg/model/foundation"
"github.com/dr-dobermann/gobpm/pkg/monitor"
"github.com/dr-dobermann/gobpm/pkg/monitor/logmon"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -45,19 +49,43 @@ func TestNewWaiter(t *testing.T) {

_, err = waiters.CreateWaiter(ep, signalEDef)
require.Error(t, err)
}

func TestTimeWaiter(t *testing.T) {
t.Run("single time",
func(t *testing.T) {
ept := mockeventproc.NewMockEventProcessor(t)
ept.EXPECT().
ProcessEvent(
mock.AnythingOfType("backgroundCtx"),
mock.Anything,
mock.Anything).
RunAndReturn(
func(_ context.Context, ed flow.EventDefinition) error {
t.Log(" >>>> got event: ", ed.Type(), " #", ed.Id())
return nil
})

// time expression
timeEDef := events.MustTimerEventDefinition(
goexpr.Must(
nil,
data.MustItemDefinition(
values.NewVariable(time.Now())),
func(_ context.Context, ds data.Source) (data.Value, error) {
return values.NewVariable(time.Now().Add(2 * time.Second)), nil
}),
nil, nil)

// monitoring
m, err := logmon.New(
slog.New(
slog.NewJSONHandler(
os.Stderr,
&slog.HandlerOptions{
Level: slog.LevelDebug,
})))
require.NoError(t, err)

w, err := waiters.CreateWaiter(ept, timeEDef)
require.NoError(t, err)
require.Equal(t, eventproc.WSReady, w.State())
Expand All @@ -66,7 +94,9 @@ func TestNewWaiter(t *testing.T) {
err = w.Stop()
require.Error(t, err)

err = w.Service(context.Background())
mCtx := context.WithValue(context.Background(), monitor.Key, m)

err = w.Service(mCtx)
require.NoError(t, err)

time.Sleep(3 * time.Second)
Expand Down

0 comments on commit ba3fcdc

Please sign in to comment.