-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): Add background publisher to MessageBus
Creates a channel of provided capacity and attaches to the sdk instance. This channel is then passed to the trigger so that MessageEnvelopes can be pulled off of it and dropped on the queue. Publisher handles formatting the passed message so there is no need to couple using types from go-mod-messaging. Closes: #462
- Loading branch information
Alex Ullrich
committed
Sep 4, 2020
1 parent
6b45ea7
commit fbe2c0a
Showing
8 changed files
with
185 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package appsdk | ||
|
||
import "github.com/edgexfoundry/go-mod-messaging/pkg/types" | ||
|
||
type BackgroundPublisher interface { | ||
Publish(payload []byte, correlationID string, contentType string) | ||
} | ||
|
||
type backgroundPublisher struct { | ||
output chan<- types.MessageEnvelope | ||
} | ||
|
||
func (pub *backgroundPublisher) Publish(payload []byte, correlationID string, contentType string) { | ||
outputEnvelope := types.MessageEnvelope{ | ||
CorrelationID: correlationID, | ||
Payload: payload, | ||
ContentType: contentType, | ||
} | ||
|
||
pub.output <- outputEnvelope | ||
} | ||
|
||
func newBackgroundPublisher(capacity int) (<-chan types.MessageEnvelope, BackgroundPublisher) { | ||
bgch := make(chan types.MessageEnvelope, capacity) | ||
return bgch, &backgroundPublisher{output: bgch} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package appsdk | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewBackgroundPublisherAndPublish(t *testing.T) { | ||
background, pub := newBackgroundPublisher(1) | ||
|
||
payload := []byte("something") | ||
correlationId := "id" | ||
contentType := "type" | ||
|
||
pub.Publish(payload, correlationId, contentType) | ||
|
||
waiting := true | ||
|
||
for waiting { | ||
select { | ||
case msgs := <-background: | ||
assert.Equal(t, correlationId, msgs.CorrelationID) | ||
assert.Equal(t, contentType, msgs.ContentType) | ||
assert.Equal(t, payload, msgs.Payload) | ||
waiting = false | ||
case <-time.After(1 * time.Second): | ||
assert.Fail(t, "Message timed out, background channel likely not configured correctly") | ||
waiting = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters