Skip to content
This repository has been archived by the owner on Aug 21, 2021. It is now read-only.

Commit

Permalink
Get tests passing with data type refactor
Browse files Browse the repository at this point in the history
This gets all of the tests passing following the data type refactor.

Incidentally, I think I also fixed the issue that was blocking travisci
from passing tests. The topic channel tests were hanging because
sometimes the message would be published before the subscriber established
its subscription. Then they'd never get the message, and they'd wait for
it forever. I added a delay to the test before the initial broadcast,
and I haven't seen it hang since.
  • Loading branch information
AusIV committed Nov 22, 2017
1 parent 276826f commit 578a53f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 60 deletions.
27 changes: 16 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ $(BASE):
@mkdir -p $(dir $@)
@ln -sf $(CURDIR) $@

clean:
clean: dockerstop
rm -rf bin/ .gopath/


dockerstop:
docker stop `cat $(BASE)/tmp/dynamo.containerid` || true
docker rm `cat $(BASE)/tmp/dynamo.containerid` || true
rm $(BASE)/tmp/dynamo.containerid || true
docker stop `cat $(BASE)/tmp/redis.containerid` || true
docker rm `cat $(BASE)/tmp/redis.containerid` || true
rm $(BASE)/tmp/redis.containerid || true

nodesetup:
cd $(BASE)/js ; npm install

Expand Down Expand Up @@ -45,11 +54,11 @@ bin: bin/delayrelay bin/fundcheckrelay bin/getbalance bin/ingest bin/initialize
truffleCompile:
cd $(BASE)/js ; node_modules/.bin/truffle compile

testredis: $(BASE)/tmp/redis.containerid
$(BASE)/tmp/redis.containerid:
mkdir -p $(BASE)/tmp
docker run -d -p 6379:6379 redis > $(BASE)/tmp/redis.containerid

testdynamo: $(BASE)/tmp/dynamo.containerid
$(BASE)/tmp/dynamo.containerid:
mkdir -p $(BASE)/tmp
docker run -d -p 8000:8000 cnadiminti/dynamodb-local > $(BASE)/tmp/dynamo.containerid

Expand All @@ -59,22 +68,18 @@ py/.env:
$(BASE)/py/.env/bin/pip install -r $(BASE)/py/requirements/indexer.txt
$(BASE)/py/.env/bin/pip install nose

gotest: testredis
gotest: $(BASE)/tmp/redis.containerid
cd $(BASE)/funds && go test
cd $(BASE)/channels && REDIS_URL=localhost:6379 go test
cd $(BASE)/accounts && REDIS_URL=localhost:6379 go test
cd $(BASE)/affiliates && REDIS_URL=localhost:6379 go test
cd $(BASE)/types && go test
cd $(BASE)/ingest && go test
docker stop `cat $(BASE)/tmp/redis.containerid`
docker rm `cat $(BASE)/tmp/redis.containerid`

pytest: testdynamo
pytest: $(BASE)/tmp/dynamo.containerid
cd $(BASE)/py && DYNAMODB_HOST="http://localhost:8000" $(BASE)/py/.env/bin/nosetests
docker stop `cat $(BASE)/tmp/dynamo.containerid`
docker rm `cat $(BASE)/tmp/dynamo.containerid`

jstest: testredis
jstest: $(BASE)/tmp/redis.containerid
cd $(BASE)/js && REDIS_URL=localhost:6379 node_modules/.bin/mocha

test: jstest gotest pytest
test: $(BASE)/tmp/dynamo.containerid $(BASE)/tmp/redis.containerid jstest gotest pytest dockerstop
5 changes: 3 additions & 2 deletions accounts/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts_test
import (
"encoding/hex"
"github.com/notegio/openrelay/accounts"
"github.com/notegio/openrelay/types"
"github.com/notegio/openrelay/config"
"gopkg.in/redis.v3"
"math/big"
Expand All @@ -28,7 +29,7 @@ func TestGetDefaultAccount(t *testing.T) {
return
}
service := accounts.NewRedisAccountService(redisClient)
account := service.Get([20]byte{})
account := service.Get(&types.Address{})
if account.Blacklisted() {
t.Errorf("Default account should not be blacklisted")
return
Expand All @@ -48,7 +49,7 @@ func TestSetAccount(t *testing.T) {
service := accounts.NewRedisAccountService(redisClient)
account := accounts.NewAccount(false, new(big.Int), 0, time.Now().Unix()+5)
address, _ := hex.DecodeString("0000000000000000000000000000000000000000")
var addressArray [20]byte
addressArray := &types.Address{}
copy(addressArray[:], address[:])
err := service.Set(addressArray, account)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions affiliates/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package affiliates_test
import (
"encoding/hex"
"github.com/notegio/openrelay/affiliates"
"github.com/notegio/openrelay/types"
"github.com/notegio/openrelay/config"
"gopkg.in/redis.v3"
"math/big"
Expand All @@ -29,7 +30,7 @@ func TestGetMissingAffiliate(t *testing.T) {
}
service := affiliates.NewRedisAffiliateService(redisClient)
address, _ := hex.DecodeString("1000000000000000000000000000000000000000")
var addressArray [20]byte
addressArray := &types.Address{}
copy(addressArray[:], address[:])
_, err := service.Get(addressArray)
if err == nil {
Expand All @@ -51,7 +52,7 @@ func TestSetAffiliate(t *testing.T) {
service := affiliates.NewRedisAffiliateService(redisClient)
affiliate := affiliates.NewAffiliate(new(big.Int), 100)
address, _ := hex.DecodeString("0000000000000000000000000000000000000000")
var addressArray [20]byte
addressArray := &types.Address{}
copy(addressArray[:], address[:])
err := service.Set(addressArray, affiliate)
if err != nil {
Expand Down
45 changes: 31 additions & 14 deletions channels/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"gopkg.in/redis.v3"
"os"
"testing"
"time"
)

type testConsumer struct {
Expand All @@ -24,14 +25,18 @@ func (consumer *testConsumer) Consume(msg channels.Delivery) {
consumer.done <- true
}

func ChannelSendTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, t *testing.T) {
func ChannelSendTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, delay time.Duration, t *testing.T) {
fmt.Println("ChannelSendTest")
// fmt.Println(publisher)
consumer := &testConsumer{make(chan string), make(chan bool), make(chan bool)}
// fmt.Println("Created consumer")
consumerChannel.AddConsumer(consumer)
// fmt.Println("Added consumer")
consumerChannel.StartConsuming()
// If a topic consumer isn't subscribed by the time we start publishing,
// it won't get the message and we'll hang forever. This delay ensures
// topic consumers have time to get subscribed.
time.Sleep(delay)
// fmt.Println("Started consumer")
publisher.Publish("test")
// fmt.Println("Published message")
Expand All @@ -44,7 +49,7 @@ func ChannelSendTest(publisher channels.Publisher, consumerChannel channels.Cons
_ = <-consumer.done
}

func ReturnUnackedTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, t *testing.T) {
func ReturnUnackedTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, delay time.Duration, t *testing.T) {
fmt.Println("ReturnUnackedTest")
// fmt.Println(publisher)
consumer := &testConsumer{make(chan string), make(chan bool), make(chan bool)}
Expand All @@ -53,6 +58,10 @@ func ReturnUnackedTest(publisher channels.Publisher, consumerChannel channels.Co
// fmt.Println("Added consumer")
consumerChannel.StartConsuming()
// fmt.Println("Started consumer")
// If a topic consumer isn't subscribed by the time we start publishing,
// it won't get the message and we'll hang forever. This delay ensures
// topic consumers have time to get subscribed.
time.Sleep(delay)
publisher.Publish("test")
// fmt.Println("Published message")
result := <-consumer.channel
Expand All @@ -69,7 +78,7 @@ func ReturnUnackedTest(publisher channels.Publisher, consumerChannel channels.Co
// fmt.Println("done here")
}

func AckTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, t *testing.T) {
func AckTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, delay time.Duration, t *testing.T) {
fmt.Println("AckTest")
// fmt.Println(publisher)
consumer := &testConsumer{make(chan string), make(chan bool), make(chan bool)}
Expand All @@ -78,6 +87,10 @@ func AckTest(publisher channels.Publisher, consumerChannel channels.ConsumerChan
// fmt.Println("Added consumer")
consumerChannel.StartConsuming()
// fmt.Println("Started consumer")
// If a topic consumer isn't subscribed by the time we start publishing,
// it won't get the message and we'll hang forever. This delay ensures
// topic consumers have time to get subscribed.
time.Sleep(delay)
publisher.Publish("test")
// fmt.Println("Published message")
result := <-consumer.channel
Expand All @@ -92,7 +105,7 @@ func AckTest(publisher channels.Publisher, consumerChannel channels.ConsumerChan
}
}

func RejectTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, t *testing.T) {
func RejectTest(publisher channels.Publisher, consumerChannel channels.ConsumerChannel, delay time.Duration, t *testing.T) {
fmt.Println("RejectTest")
// fmt.Println(publisher)
consumer := &testConsumer{make(chan string), make(chan bool), make(chan bool)}
Expand All @@ -101,6 +114,10 @@ func RejectTest(publisher channels.Publisher, consumerChannel channels.ConsumerC
// fmt.Println("Added consumer")
consumerChannel.StartConsuming()
// fmt.Println("Started consumer")
// If a topic consumer isn't subscribed by the time we start publishing,
// it won't get the message and we'll hang forever. This delay ensures
// topic consumers have time to get subscribed.
time.Sleep(delay)
publisher.Publish("test")
// fmt.Println("Published message")
result := <-consumer.channel
Expand All @@ -120,19 +137,19 @@ func RejectTest(publisher channels.Publisher, consumerChannel channels.ConsumerC

func TestMockChannelSend(t *testing.T) {
publisher, consumerChannel := channels.MockChannel()
ChannelSendTest(publisher, consumerChannel, t)
ChannelSendTest(publisher, consumerChannel, 0, t)
}
func TestMockReturnUnacked(t *testing.T) {
publisher, consumerChannel := channels.MockChannel()
ReturnUnackedTest(publisher, consumerChannel, t)
ReturnUnackedTest(publisher, consumerChannel, 0, t)
}
func TestMockAck(t *testing.T) {
publisher, consumerChannel := channels.MockChannel()
AckTest(publisher, consumerChannel, t)
AckTest(publisher, consumerChannel, 0, t)
}
func TestMockReject(t *testing.T) {
publisher, consumerChannel := channels.MockChannel()
RejectTest(publisher, consumerChannel, t)
RejectTest(publisher, consumerChannel, 0, t)
}

func redisCleanup(redisClient *redis.Client, consumerChannel channels.ConsumerChannel) {
Expand All @@ -155,7 +172,7 @@ func TestRedisQueueChannelSend(t *testing.T) {
publisher := channels.NewRedisQueuePublisher("test_queue", redisClient)
consumerChannel := channels.NewQueueConsumerChannel("test_queue", redisClient)
defer redisCleanup(redisClient, consumerChannel)
ChannelSendTest(publisher, consumerChannel, t)
ChannelSendTest(publisher, consumerChannel, 0, t)
}
func TestRedisQueueReturnUnacked(t *testing.T) {
redisURL := os.Getenv("REDIS_URL")
Expand All @@ -169,7 +186,7 @@ func TestRedisQueueReturnUnacked(t *testing.T) {
publisher := channels.NewRedisQueuePublisher("test_queue", redisClient)
consumerChannel := channels.NewQueueConsumerChannel("test_queue", redisClient)
defer redisCleanup(redisClient, consumerChannel)
ReturnUnackedTest(publisher, consumerChannel, t)
ReturnUnackedTest(publisher, consumerChannel, 0, t)
}
func TestRedisQueueAck(t *testing.T) {
redisURL := os.Getenv("REDIS_URL")
Expand All @@ -183,7 +200,7 @@ func TestRedisQueueAck(t *testing.T) {
publisher := channels.NewRedisQueuePublisher("test_queue", redisClient)
consumerChannel := channels.NewQueueConsumerChannel("test_queue", redisClient)
defer redisCleanup(redisClient, consumerChannel)
AckTest(publisher, consumerChannel, t)
AckTest(publisher, consumerChannel, 0, t)
}
func TestRedisQueueReject(t *testing.T) {
redisURL := os.Getenv("REDIS_URL")
Expand All @@ -197,7 +214,7 @@ func TestRedisQueueReject(t *testing.T) {
publisher := channels.NewRedisQueuePublisher("test_queue", redisClient)
consumerChannel := channels.NewQueueConsumerChannel("test_queue", redisClient)
defer redisCleanup(redisClient, consumerChannel)
RejectTest(publisher, consumerChannel, t)
RejectTest(publisher, consumerChannel, 0, t)
}

func TestRedisTopicChannelSend(t *testing.T) {
Expand All @@ -212,7 +229,7 @@ func TestRedisTopicChannelSend(t *testing.T) {
publisher := channels.NewRedisTopicPublisher("test_topic", redisClient)
consumerChannel := channels.NewTopicConsumerChannel("test_topic", redisClient)
defer consumerChannel.StopConsuming()
ChannelSendTest(publisher, consumerChannel, t)
ChannelSendTest(publisher, consumerChannel, 1 * time.Second, t)
}
func TestRedisTopicAck(t *testing.T) {
redisURL := os.Getenv("REDIS_URL")
Expand All @@ -226,5 +243,5 @@ func TestRedisTopicAck(t *testing.T) {
publisher := channels.NewRedisTopicPublisher("test_topic", redisClient)
consumerChannel := channels.NewTopicConsumerChannel("test_topic", redisClient)
defer consumerChannel.StopConsuming()
AckTest(publisher, consumerChannel, t)
AckTest(publisher, consumerChannel, 1 * time.Second, t)
}
12 changes: 6 additions & 6 deletions funds/mock_balance_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

type mockBalanceChecker struct {
balances map[*types.Address]map[*types.Address]*big.Int
balances map[types.Address]map[types.Address]*big.Int
}

func (funds *mockBalanceChecker) GetBalance(tokenAddrBytes, userAddrBytes *types.Address) (*big.Int, error) {
if tokenMap, ok := funds.balances[tokenAddrBytes]; ok {
if balance, ok := tokenMap[userAddrBytes]; ok {
if tokenMap, ok := funds.balances[*tokenAddrBytes]; ok {
if balance, ok := tokenMap[*userAddrBytes]; ok {
return balance, nil
}
return nil, errors.New("(GetBalance) User address not found " + hex.EncodeToString(userAddrBytes[:]))
Expand All @@ -25,16 +25,16 @@ func (funds *mockBalanceChecker) GetAllowance(tokenAddrBytes, userAddrBytes, sen
// For now I'm just making GetAllowance match GetBalance for the mock version.
// Eventually we'll need to test differences between allowance and balance, but
// for now this will do.
if tokenMap, ok := funds.balances[tokenAddrBytes]; ok {
if balance, ok := tokenMap[userAddrBytes]; ok {
if tokenMap, ok := funds.balances[*tokenAddrBytes]; ok {
if balance, ok := tokenMap[*userAddrBytes]; ok {
return balance, nil
}
return nil, errors.New("(GetAllowance) User address not found " + hex.EncodeToString(userAddrBytes[:]))
}
return nil, errors.New("(GetAllowance) Token not found " + hex.EncodeToString(tokenAddrBytes[:]))
}

func NewMockBalanceChecker(balanceMap map[*types.Address]map[*types.Address]*big.Int) BalanceChecker {
func NewMockBalanceChecker(balanceMap map[types.Address]map[types.Address]*big.Int) BalanceChecker {
return &mockBalanceChecker{balanceMap}
}

Expand Down
Loading

0 comments on commit 578a53f

Please sign in to comment.