Address: interface and implementation
This commit is contained in:
		
							parent
							
								
									ea91a0486e
								
							
						
					
					
						commit
						e1bc2322f3
					
				
					 5 changed files with 91 additions and 11 deletions
				
			
		|  | @ -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 { | ||||
|  |  | |||
|  | @ -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 | ||||
| } | ||||
|  |  | |||
|  | @ -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() | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
							
								
								
									
										35
									
								
								tests/unit_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								tests/unit_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -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") | ||||
| } | ||||
		Loading…
	
	Add table
		
		Reference in a new issue