Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Announcements #268

Open
dtornow opened this issue Apr 1, 2024 · 3 comments
Open

Add Announcements #268

dtornow opened this issue Apr 1, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@dtornow
Copy link
Contributor

dtornow commented Apr 1, 2024

Describe the problem you are facing

Resonate's Deterministic Simulation Testing cannot observe the Resonate Server beyond the request/response API. E.g. the Deterministic Simulation Testing cannot observe if notifications are actually sent.

Describe the solution you'd like

P-Lang style announcements & monitors. With announcements and monitors, DST will be able to verify that e.g. announcements are sent if a corresponding subscription exists.

Implementation Details

Step 1

Create a package announcements.

Step 2

Define an interface Announcement with a function

type Announcement interface {
    Announce(data map[string]string)
}

Step 3

Define two different implementations:

  • NopAnnouncement
  • DstAnnouncement

Step 4

Use a package-level private variable to hold a singleton instance. Instantiate the correct singleton instance based on config parameter on startup e.g.

package announcements

import "sync"

var (
    instance Announcement
    once     sync.Once
)

func Initialize(envType EnvironmentType) {
    once.Do(func() {
        switch envType {
        case Nop:
            instance = &NopAnnouncement{}
        case Dst:
            instance = &DstAnnouncement{}
        default:
            // Handle default case or throw an error
        }
    })
}

// Additional code to define EnvironmentType, NopAnnouncement, and DstAnnouncement follows...

Step 5

Implement the interface

  • Nop This will simply implement the Announce method without performing any operations.
  • Dst This will need to safely add the announcement data to an array for later verification. Ensure thread safety
@dtornow dtornow added the enhancement New feature or request label Apr 1, 2024
@dtornow
Copy link
Contributor Author

dtornow commented Apr 1, 2024

The network is a good candidate to add a first announcement

return fmt.Sprintf("Network(http=Http(method=%s, url=%s))", s.Http.Method, s.Http.Url)

@dtornow
Copy link
Contributor Author

dtornow commented Apr 2, 2024

Add a custom event struct. The only required parameter is type, everything else is a key value map

type Event struct {
	Type string
	data map[string]interface{}
}


func NewEvent(type string, initialData ...map[string]interface{}) *Event {
	var data map[string]interface{}
	if len(initialData) > 0 {
		data = initialData[0] // Use the first map provided if any.
	} else {
		data = make(map[string]interface{}) 
	}

	return &Event{
		Type: eventType,
		data: data
	}
}

// Set adds or updates a key with a given value.
func (e *Event) Set(key string, value interface{}) {
	e.data[key] = value
}

// Get tries to retrieve a value of the specified type T from the event data. It returns the zero value and an error if the type does not match.
func [T any](e *Event) Get[T any](key string) (T, error) {
	value, exists := e.data[key]
	if !exists {
		var zero T
		panic("key %s does not exist in event of type %s", key, e.Type)
	}

	typedValue, ok := value.(T)
	if !ok {
		panic("value at key %s is not of the requested type in event of type %s", key, e.Type)
	}

	return typedValue, nil
}

@dtornow
Copy link
Contributor Author

dtornow commented Apr 2, 2024

Regarding announcements: The announcement (the event) should contain all (relevant) application level information. For example, in the context of networking, the event should contain the url, http method, and http body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants