Skip to content

Commit

Permalink
calling timer the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Nov 17, 2017
1 parent 162f453 commit da6f4b1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
10 changes: 4 additions & 6 deletions security/usage/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ Metering = new(HTTPStorage)

// HTTPStorage represents a usage storage which posts meters over HTTP.
type HTTPStorage struct {
counters *sync.Map // The counters map.
counters sync.Map // The counters map.
url string // The url to post to.
http http.Client // The http client to use.
done chan bool // The closing channel.
Expand All @@ -63,8 +63,7 @@ type HTTPStorage struct {
// NewHTTP creates a new HTTP storage
func NewHTTP() *HTTPStorage {
return &HTTPStorage{
counters: new(sync.Map),
done: make(chan bool),
done: make(chan bool),
}
}

Expand All @@ -90,10 +89,9 @@ func (s *HTTPStorage) Configure(config map[string]interface{}) (err error) {
// Get the url from the provider configuration
if url, ok := config["url"]; ok {
s.url = url.(string)
utils.Repeat(s.store, interval, s.done) // TODO: closing chan

// Create a new HTTP client to use
s.http, err = http.NewClient(s.url, 30*time.Second)

utils.Repeat(s.store, interval, s.done) // TODO: closing chan
return
}

Expand Down
33 changes: 18 additions & 15 deletions security/usage/metering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ func TestHTTP_Name(t *testing.T) {
assert.Equal(t, "http", s.Name())
}

func TestHTTP_ConfigureErr(t *testing.T) {
s := NewHTTP()
close(s.done)

err := s.Configure(nil)
assert.Error(t, errors.New("Configuration was not provided for HTTP metering provider"), err)
}

func TestHTTP_Configure(t *testing.T) {
s := NewHTTP()
defer close(s.done)
close(s.done)

err := s.Configure(map[string]interface{}{
"interval": 1000.0,
"url": "http://localhost/test",
})
assert.NoError(t, err)
assert.Equal(t, "http://localhost/test", s.url)
assert.NotNil(t, s.http)

{
err := s.Configure(nil)
assert.Error(t, errors.New("Configuration was not provided for HTTP metering provider"), err)
}

{
err := s.Configure(map[string]interface{}{
"interval": 1000.0,
"url": "http://localhost/test",
})
assert.NoError(t, err)
assert.Equal(t, "http://localhost/test", s.url)
assert.NotNil(t, s.http)
}
}

func TestHTTP_Store(t *testing.T) {
Expand All @@ -67,6 +69,7 @@ func TestHTTP_Store(t *testing.T) {
u2 := usage{MessageIn: 0, TrafficIn: 0, MessageEg: 0, TrafficEg: 0, Contract: 0x1}

s := NewHTTP()
defer close(s.done)
s.http = h

c := s.Get(1)
Expand Down
1 change: 1 addition & 0 deletions utils/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// Repeat creates a ticker which tickes periodically
func Repeat(action func(), interval time.Duration, closing chan bool) *time.Ticker {
action()
timer := time.NewTicker(interval)
go func() {
for {
Expand Down

0 comments on commit da6f4b1

Please sign in to comment.