diff --git a/lib/lib.go b/lib/lib.go index 34e8b74..d1413eb 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -47,8 +47,11 @@ type Context interface { } type Address interface { + Url() string Service() string - Send(Context, Message) + Interaction() (string, string) + SetUrl(string) + SetInteraction(string, string) } type Message interface { diff --git a/lib/message/message.go b/lib/message/message.go index 2650eaf..a24593e 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -1,6 +1,11 @@ package message -import "git.sr.ht/~cco/go-scopes/lib" +import ( + "fmt" + "strings" + + "git.sr.ht/~cco/go-scopes/lib" +) // Message implementation(s) @@ -23,19 +28,56 @@ var Quit = StrMessage("quit") // Address implementation type address struct { - service string + url, srv, sid, iid string +} + +func (addr *address) String() string { + return fmt.Sprintf("%s#%s/%s/%s", addr.url, addr.srv, addr.sid, addr.iid) +} + +func (addr *address) Url() string { + return addr.url } func (addr *address) Service() string { - return addr.service + return addr.srv } -func (addr *address) Send(ctx lib.Context, msg lib.Message) { - lib.Send(ctx, addr, msg) +func (addr *address) Interaction() (sid, iid string) { + return addr.sid, addr.iid +} + +func (addr *address) SetUrl(url string) { + addr.url = url +} + +func (addr *address) SetInteraction(sid, iid string) { + addr.sid = sid + addr.iid = iid } func SimpleAddress(srv string) *address { return &address{ - service: srv, + srv: srv, } } + +func ParseAddress(astr string) *address { + addr := address{} + p1 := strings.SplitN(astr, "#", 2) + s1 := p1[0] + s2 := s1 + if len(p1) > 1 { + addr.url = s1 + s2 = p1[1] + } + p2 := strings.Split(s2, "/") + addr.srv = p2[0] + if len(p2) > 1 { + addr.sid = p2[1] + if len(p2) > 2 { + addr.iid = p2[2] + } + } + return &addr +} diff --git a/testing/testing.go b/testing/testing.go index 7b0c0d2..0ffd76d 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -55,12 +55,12 @@ func (t *T) TearDownApp() { // give actors time to recieve all messages: time.Sleep(100 * time.Millisecond) //t.Check() - message.SimpleAddress("testing").Send(t.Ctx, message.Quit) + lib.Send(t.Ctx, message.SimpleAddress("testing"), message.Quit) t.Ctx.WaitGroup().Wait() //t.AssertNoUncheckedMessages() } -func (t *T) LogCount(pr bool) int { +func (t *T) LogCheck(pr bool) int { count := 0 f, _ := os.Open("log/scopes.log") defer f.Close() diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 0a5353c..fb7108c 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -11,7 +11,7 @@ import ( "git.sr.ht/~cco/go-scopes/tests/etc" ) -func TestConfig(tb *tbase.T) { +func TestScopesApp(tb *tbase.T) { os.Remove("log/scopes.log") // make sure we start with a fresh log file t := testing.SetUpApp(tb, etc.Config()) t.Run("app", AppTest) @@ -19,7 +19,7 @@ func TestConfig(tb *tbase.T) { t.Run("send", SendTest) t.Run("client", ClientTest) t.TearDownApp() - t.AssertEqual(t.LogCount(true), 12) + t.AssertEqual(t.LogCheck(true), 12) } func AppTest(t *testing.T) { diff --git a/tests/unit_test.go b/tests/unit_test.go new file mode 100644 index 0000000..79da687 --- /dev/null +++ b/tests/unit_test.go @@ -0,0 +1,35 @@ +package scopes_test + +import ( + "fmt" + tbase "testing" + + "git.sr.ht/~cco/go-scopes/lib" + "git.sr.ht/~cco/go-scopes/lib/message" + "git.sr.ht/~cco/go-scopes/testing" +) + +func TestUnit(tb *tbase.T) { + t := testing.SetUp(tb) + t.Run("address", AddressTest) +} + +func AddressTest(t *testing.T) { + var ad1 lib.Address = message.SimpleAddress("worker") + t.AssertEqual(fmt.Sprint(ad1), "#worker//") + astr := "https://scopes.cy7.eu#server/1abc/2cde" + var ad2 lib.Address = message.ParseAddress(astr) + t.AssertEqual(fmt.Sprint(ad2), astr) + t.AssertEqual(fmt.Sprint(message.ParseAddress("worker")), "#worker//") + t.AssertEqual(ad1.Url(), "") + t.AssertEqual(ad2.Url(), "https://scopes.cy7.eu") + sid, iid := ad1.Interaction() + t.AssertEqual(sid, "") + t.AssertEqual(iid, "") + sid, iid = ad2.Interaction() + t.AssertEqual(sid, "1abc") + t.AssertEqual(iid, "2cde") + ad1.SetUrl("https://scopes.cy7.eu") + ad1.SetInteraction("1abc", "2cde") + t.AssertEqual(fmt.Sprint(ad1), "https://scopes.cy7.eu#worker/1abc/2cde") +}