Skip to content

Commit

Permalink
URLClient tests pass.
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Forster <me@brandonforster.com>
  • Loading branch information
brandonforster committed Jan 31, 2020
1 parent fec087c commit 2e62e9e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
2 changes: 1 addition & 1 deletion clients/interfaces/urlstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package interfaces

type URLStream chan string
type URLStream string
8 changes: 2 additions & 6 deletions clients/urlclient/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@

package urlclient

import (
"github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces"
)

// localClient defines a ClientURL implementation that returns the struct field for the URL.
type localClient struct {
url string
}

// newLocalClient returns a pointer to a localClient.
func newLocalClient(stream interfaces.URLStream) *localClient {
func newLocalClient(url string) *localClient {
return &localClient{
url: <-stream,
url: url,
}
}

Expand Down
7 changes: 2 additions & 5 deletions clients/urlclient/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ package urlclient

import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/clients/types"
)

func TestNewLocalClient(t *testing.T) {
actualClient := newLocalClient(types.EndpointParams{UseRegistry: false})
actualClient := newLocalClient(expectedURL)

if actualClient == nil {
t.Fatal("nil returned from newLocalClient")
}
}

func TestLocalClient_URLPrefix(t *testing.T) {
expectedURL := "http://domain.com"
urlClient := newLocalClient(types.EndpointParams{Url: expectedURL})
urlClient := newLocalClient(expectedURL)

actualURL, err := urlClient.Prefix()

Expand Down
8 changes: 5 additions & 3 deletions clients/urlclient/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ type registryClient struct {

// NewRegistryClient returns a pointer to a registryClient.
// A pointer is used so that when using configuration from a registry, the Prefix can be updated asynchronously.
func NewRegistryClient(urlStream interfaces.URLStream, strategy urlClientInterfaces.RetryStrategy) *registryClient {
func NewRegistryClient(
urlStream chan interfaces.URLStream,
strategy urlClientInterfaces.RetryStrategy) *registryClient {
c := registryClient{
strategy: strategy,
}

go func(ch chan string) {
go func(ch chan interfaces.URLStream) {
for {
select {
case url := <-ch:
c.url = url
c.url = string(url)
strategy.SetLock(false)
}
}
Expand Down
56 changes: 20 additions & 36 deletions clients/urlclient/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
package urlclient

import (
"fmt"
"testing"
"time"

"github.com/edgexfoundry/go-mod-core-contracts/clients/types"
interfaces2 "github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/errors"
"github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/retry"
)

var timeoutError = errors.NewTimeoutError()
var expectedURL = "http://domain.com"

func TestNewRegistryClient(t *testing.T) {
actualClient := NewRegistryClient(
types.EndpointParams{UseRegistry: true},
mockEndpoint{},
retry.NewPeriodicRetry(types.URLClientParams{Interval: 500, Timeout: 10}),
makeTestStream(),
retry.NewPeriodicRetry(500, 10),
)

if actualClient == nil {
Expand All @@ -40,16 +39,14 @@ func TestNewRegistryClient(t *testing.T) {
}

func TestRegistryClient_Prefix_Periodic(t *testing.T) {
strategy := retry.NewPeriodicRetry(types.URLClientParams{Interval: 500, Timeout: 10})
strategy := retry.NewPeriodicRetry(500, 10)
testStream := makeTestStream()

expectedURL := "http://domain.com"
testEndpoint := mockEndpoint{ch: make(chan string, 1)}
urlClient := NewRegistryClient(
types.EndpointParams{},
testEndpoint,
testStream,
strategy,
)
testEndpoint.SendToChannel()
testStream <- interfaces2.URLStream(expectedURL)

// don't sleep, we need to actuate the retry code

Expand All @@ -66,17 +63,15 @@ func TestRegistryClient_Prefix_Periodic(t *testing.T) {

func TestRegistryClient_Prefix_Periodic_Initialized(t *testing.T) {
// use impossible timing to ensure that if hit, the retry logic will error out
strategy := retry.NewPeriodicRetry(types.URLClientParams{Interval: 100000000, Timeout: 10})
strategy := retry.NewPeriodicRetry(100000000, 10)
testStream := makeTestStream()

expectedURL := "http://domain.com"
testEndpoint := mockEndpoint{ch: make(chan string, 1)}
urlClient := NewRegistryClient(
types.EndpointParams{},
testEndpoint,
testStream,
strategy,
)

testEndpoint.SendToChannel()
testStream <- interfaces2.URLStream(expectedURL)

// sleep so that the retry code doesn't run and we only execute the shortcut
sleep(t, strategy)
Expand All @@ -94,9 +89,8 @@ func TestRegistryClient_Prefix_Periodic_Initialized(t *testing.T) {

func TestRegistryClient_Prefix_Periodic_TimedOut(t *testing.T) {
urlClient := NewRegistryClient(
types.EndpointParams{},
mockEndpoint{},
retry.NewPeriodicRetry(types.URLClientParams{Interval: 100000000, Timeout: 1}),
makeTestStream(),
retry.NewPeriodicRetry(100000000,1),
)

actualURL, err := urlClient.Prefix()
Expand All @@ -112,15 +106,13 @@ func TestRegistryClient_Prefix_Periodic_TimedOut(t *testing.T) {

func TestRegistryClient_Prefix_Once(t *testing.T) {
strategy := retry.NewOnce()
testStream := makeTestStream()

expectedURL := "http://domain.com"
testEndpoint := mockEndpoint{ch: make(chan string, 1)}
urlClient := NewRegistryClient(
types.EndpointParams{},
testEndpoint,
testStream,
strategy,
)
testEndpoint.SendToChannel()
testStream <- interfaces2.URLStream(expectedURL)

sleep(t, strategy)

Expand All @@ -137,8 +129,7 @@ func TestRegistryClient_Prefix_Once(t *testing.T) {

func TestRegistryClient_Prefix_Once_NotAvailable(t *testing.T) {
urlClient := NewRegistryClient(
types.EndpointParams{},
mockEndpoint{},
makeTestStream(),
retry.NewOnce(),
)

Expand All @@ -163,14 +154,7 @@ func sleep(t *testing.T, strategy interfaces.RetryStrategy) {
}
}

type mockEndpoint struct {
ch chan string
func makeTestStream() chan interfaces2.URLStream {
return make(chan interfaces2.URLStream)
}

func (e mockEndpoint) Monitor(_ types.EndpointParams) chan string {
return e.ch
}

func (e mockEndpoint) SendToChannel() {
e.ch <- fmt.Sprint("http://domain.com")
}

0 comments on commit 2e62e9e

Please sign in to comment.