Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Adds Send method to Address
Browse files Browse the repository at this point in the history
  • Loading branch information
glorieux committed Oct 21, 2019
1 parent 84c16e3 commit f986753
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
16 changes: 11 additions & 5 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import (
"os"
"strings"

"github.com/mustafaturan/bus"
"pkg.glorieux.io/mantra/internal/log"
)

// Addressable means a type has an Address
type Addressable interface {
Address() *Address
}

// Address is a service's address
type Address struct {
// TODO find a better representation
Expand Down Expand Up @@ -46,6 +42,16 @@ func parseAddress(a string) *Address {
}
}

// Send sends a message
func (a *Address) Send(method string, args ...interface{}) {
topic := newTopic(a, method)
log.Debug("TOPIC: ", topic)
_, err := bus.Emit(topic.String(), args, "")
if err != nil {
log.Error(err)
}
}

func (a *Address) String() string {
return fmt.Sprintf(
"%d@%s#%s",
Expand Down
1 change: 0 additions & 1 deletion internal/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func TestName(t *testing.T) {
testInt := TestInt(42)
assert.Equal(t, "TestInt", structs.Name(testInt))
})

}

func TestMethods(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions mantra.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Stop() {
}

// Send supperseeds Send
// Deprecated: use it from address directly
func Send(address *Address, method interface{}, args ...interface{}) {
topic := newTopic(address, structs.FuncName(method))
log.Debug("TOPIC: ", topic)
Expand Down
5 changes: 3 additions & 2 deletions mantra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func TestServiceCommunication(t *testing.T) {
t.Error(err)
}

ts := mantra.Lookup("testService")
ts.Send("TestWithoutParams")
ack := make(chan string)
mantra.Send(mantra.Lookup("testService"), ts1.TestWithoutParams)
mantra.Send(mantra.Lookup("testService"), ts1.TestMessage, ack)
ts.Send("TestMessage", ack)
assert.NotEmpty(t, <-ack)
mantra.Stop()
}
3 changes: 2 additions & 1 deletion sms/sms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestMain(m *testing.M) {
func TestSend(t *testing.T) {
s := sms.New("", "", func(*sms.Message) error { return nil })
mantra.New(s)
mantra.Send(mantra.Lookup("ShortMessageService"), s.Send, &sms.Message{})
shortMessageService := mantra.Lookup("ShortMessageService")
shortMessageService.Send("Send", &sms.Message{})
mantra.Stop()
}

0 comments on commit f986753

Please sign in to comment.