Skip to content

Commit

Permalink
Initial commit for environment test (#254)
Browse files Browse the repository at this point in the history
Signed-off-by: ROHITH RAJU <rohithraju488@gmail.com>
Co-authored-by: Victor Gaydov <victor@enise.org>
  • Loading branch information
Rohith-Raju and gavv committed Feb 8, 2023
1 parent c421815 commit 884a01c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
40 changes: 37 additions & 3 deletions environment.go
Expand Up @@ -2,6 +2,7 @@ package httpexpect

import (
"errors"
"sync"
"time"
)

Expand All @@ -13,9 +14,9 @@ import (
// env.Put("key", "value")
// value := env.GetString("key")
type Environment struct {
noCopy noCopy
chain *chain
data map[string]interface{}
mu sync.RWMutex
chain *chain
data map[string]interface{}
}

// NewEnvironment returns a new Environment.
Expand Down Expand Up @@ -58,6 +59,9 @@ func (e *Environment) Put(key string, value interface{}) {
opChain := e.chain.enter("Put(%q)", key)
defer opChain.leave()

e.mu.Lock()
defer e.mu.Unlock()

e.data[key] = value
}

Expand All @@ -72,6 +76,9 @@ func (e *Environment) Delete(key string) {
opChain := e.chain.enter("Delete(%q)", key)
defer opChain.leave()

e.mu.Lock()
defer e.mu.Unlock()

delete(e.data, key)
}

Expand All @@ -86,6 +93,9 @@ func (e *Environment) Has(key string) bool {
opChain := e.chain.enter("Has(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

_, ok := e.data[key]
return ok
}
Expand All @@ -102,6 +112,9 @@ func (e *Environment) Get(key string) interface{} {
opChain := e.chain.enter("Get(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, _ := envValue(opChain, e.data, key)

return value
Expand All @@ -118,6 +131,9 @@ func (e *Environment) GetBool(key string) bool {
opChain := e.chain.enter("GetBool(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return false
Expand Down Expand Up @@ -150,6 +166,9 @@ func (e *Environment) GetInt(key string) int {
opChain := e.chain.enter("GetInt(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return 0
Expand Down Expand Up @@ -235,6 +254,9 @@ func (e *Environment) GetFloat(key string) float64 {
opChain := e.chain.enter("GetFloat(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return 0
Expand Down Expand Up @@ -275,6 +297,9 @@ func (e *Environment) GetString(key string) string {
opChain := e.chain.enter("GetString(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return ""
Expand Down Expand Up @@ -306,6 +331,9 @@ func (e *Environment) GetBytes(key string) []byte {
opChain := e.chain.enter("GetBytes(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return nil
Expand Down Expand Up @@ -338,6 +366,9 @@ func (e *Environment) GetDuration(key string) time.Duration {
opChain := e.chain.enter("GetDuration(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return time.Duration(0)
Expand Down Expand Up @@ -370,6 +401,9 @@ func (e *Environment) GetTime(key string) time.Time {
opChain := e.chain.enter("GetTime(%q)", key)
defer opChain.leave()

e.mu.RLock()
defer e.mu.RUnlock()

value, ok := envValue(opChain, e.data, key)
if !ok {
return time.Unix(0, 0)
Expand Down
17 changes: 17 additions & 0 deletions environment_test.go
Expand Up @@ -32,6 +32,23 @@ func TestEnvironment_Constructors(t *testing.T) {
})
}

func TestEnvironment_Reentrant(t *testing.T) {
reporter := newMockReporter(t)

env := NewEnvironment(reporter)

reportCalled := false
reporter.reportCb = func() {
env.Put("good_key", 123)
reportCalled = true
}

env.Get("bad_key")
env.chain.assertFailed(t)

assert.True(t, reportCalled)
}

func TestEnvironment_Basic(t *testing.T) {
env := newEnvironment(newMockChain(t))

Expand Down
5 changes: 5 additions & 0 deletions mocks_test.go
Expand Up @@ -183,6 +183,7 @@ func (l *mockLogger) Logf(message string, args ...interface{}) {
type mockReporter struct {
testing *testing.T
reported bool
reportCb func()
}

func newMockReporter(t *testing.T) *mockReporter {
Expand All @@ -192,6 +193,10 @@ func newMockReporter(t *testing.T) *mockReporter {
func (r *mockReporter) Errorf(message string, args ...interface{}) {
r.testing.Logf("Fail: "+message, args...)
r.reported = true

if r.reportCb != nil {
r.reportCb()
}
}

type mockFormatter struct {
Expand Down

0 comments on commit 884a01c

Please sign in to comment.