dapr_wrapper

package module
v0.0.0-...-a7aa9a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 13 Imported by: 0

README

Dapr integration testing wrapper

This a naive wrapper around Dapr for integration testing your Dapr applications.

Installation

go get github.com/amikos-tech/darpper
Assumptions
  • Docker is installed on the machine (used for self-hosted Dapr)
  • Dapr CLI is installed on the machine and on the PATH

Usage


package main

import (
	"context"
	dapr "github.com/dapr/go-sdk/client"
	"github.com/dapr/go-sdk/service/common"
	"github.com/stretchr/testify/require"
	dwrap "github.com/amikos-tech/drapper"
	"testing"
	"time"
)

func TestEventH(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	// Create a mock subscription handler
	dwrap.EnsureDapr()
	eventReceived := make(chan bool)
	handler := func(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
		t.Logf("Received event: %s %v\n", e.Type, e.Data)
		require.Equal(t, map[string]interface{}{"data": "hello world"}, e.Data)
		eventReceived <- true
		return false, nil
	}

	closeEvent, err := dwrap.RegisterEventHandler(ctx, "myapp", "pubsub", "search-result", "/search", "./components", handler)
	require.NoError(t, err)
	daprClient, err := dapr.NewClient()
	require.NoError(t, err, "Error creating Dapr client: %v\n", err)

	err = daprClient.PublishEvent(context.Background(), "pubsub", "search-result", []byte(`{"data": "hello world"}`), dapr.PublishEventWithMetadata(map[string]string{"cloudevent.type": "test"}))
	require.NoError(t, err, "Error publishing event: %v\n", err)
	t.Cleanup(func() {
		if closeEvent != nil {
			closeEvent()
		}
		cancel()
	})
	select {
	case <-eventReceived:
		t.Log("Event received successfully")
	case <-time.After(10 * time.Second): // Adjust timeout as necessary
		t.Fatalf("Timed out waiting for event")
	}

}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureDapr

func EnsureDapr()

func RegisterEventHandler

func RegisterEventHandler(ctx context.Context, appID string, pubsubName string, topic string, route string, resourcePath string, handler func(ctx context.Context, e *common.TopicEvent) (retry bool, err error)) (cancel func(), err error)

Types

type DaprService

type DaprService struct {
	AppID        string
	AppPort      int
	DaprHTTPPort int
	DaprGRPCPort int
	Command      *exec.Cmd
	Handlers     map[string]common.ServiceInvocationHandler
	Service      common.Service
	Wg           sync.WaitGroup
}

func (*DaprService) Start

func (s *DaprService) Start() (dapr.Client, error)

func (*DaprService) Stop

func (s *DaprService) Stop() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL