Skip to content

Commit

Permalink
Fix unit tests + remove excessive logging
Browse files Browse the repository at this point in the history
  • Loading branch information
leszko committed Oct 18, 2023
1 parent 2f08b09 commit 0f621ac
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
17 changes: 17 additions & 0 deletions core/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ func TestReserve(t *testing.T) {
assert.Zero(big.NewRat(0, 1).Cmp(b.Balance(mid)))
}

func TestFixedPrice(t *testing.T) {
b := NewBalances(5 * time.Second)
id1 := ManifestID("12345")
id2 := ManifestID("abcdef")

// No fixed price set yet
assert.Nil(t, b.FixedPrice(id1))

// Set fixed price
p := big.NewRat(1, 5)
b.SetFixedPrice(id1, p)
assert.Equal(t, p, b.FixedPrice(id1))

// No fixed price for a different manifest ID
assert.Nil(t, b.FixedPrice(id2))
}

func TestBalancesCleanup(t *testing.T) {
b := NewBalances(5 * time.Second)
assert := assert.New(t)
Expand Down
26 changes: 13 additions & 13 deletions core/orch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ func TestPriceInfo(t *testing.T) {
recipient.On("TxCostMultiplier", mock.Anything).Return(txMultiplier, nil)
orch := NewOrchestrator(n, nil)

priceInfo, err := orch.PriceInfo(ethcommon.Address{})
priceInfo, err := orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err := common.PriceToFixed(expPricePerPixel)
Expand All @@ -1410,7 +1410,7 @@ func TestPriceInfo(t *testing.T) {
orch = NewOrchestrator(n, nil)
expPricePerPixel = big.NewRat(1010, 100)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err = common.PriceToFixed(expPricePerPixel)
Expand All @@ -1425,7 +1425,7 @@ func TestPriceInfo(t *testing.T) {
orch = NewOrchestrator(n, nil)
expPricePerPixel = big.NewRat(101, 1000)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err = common.PriceToFixed(expPricePerPixel)
Expand All @@ -1439,7 +1439,7 @@ func TestPriceInfo(t *testing.T) {
orch = NewOrchestrator(n, nil)
expPricePerPixel = big.NewRat(2525, 1000)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err = common.PriceToFixed(expPricePerPixel)
Expand All @@ -1458,7 +1458,7 @@ func TestPriceInfo(t *testing.T) {
orch = NewOrchestrator(n, nil)
expPricePerPixel = big.NewRat(11, 1)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err = common.PriceToFixed(expPricePerPixel)
Expand All @@ -1477,7 +1477,7 @@ func TestPriceInfo(t *testing.T) {
orch = NewOrchestrator(n, nil)
expPricePerPixel = big.NewRat(1100, 10)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err = common.PriceToFixed(expPricePerPixel)
Expand All @@ -1496,7 +1496,7 @@ func TestPriceInfo(t *testing.T) {
orch = NewOrchestrator(n, nil)
expPricePerPixel = big.NewRat(20, 1)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)))
fixedPrice, err = common.PriceToFixed(expPricePerPixel)
Expand All @@ -1509,7 +1509,7 @@ func TestPriceInfo(t *testing.T) {
n.SetBasePrice("default", big.NewRat(0, 1))
orch = NewOrchestrator(n, nil)

priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Zero(priceInfo.PricePerUnit)
assert.Equal(int64(1), priceInfo.PixelsPerUnit)
Expand All @@ -1527,7 +1527,7 @@ func TestPriceInfo(t *testing.T) {
overhead := new(big.Rat).Add(big.NewRat(1, 1), new(big.Rat).Inv(txMultiplier))
expPricePerPixel = new(big.Rat).Mul(basePrice, overhead) // 23953749205332825000/926899968213313
require.Equal(expPricePerPixel.Num().Cmp(big.NewInt(int64(math.MaxInt64))), 1)
priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
// for this case price will be rounded when converting to fixed
assert.NotEqual(expPricePerPixel.Cmp(big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit)), 0)
Expand All @@ -1543,7 +1543,7 @@ func TestPriceInfo(t *testing.T) {

// Now make sure when AutoAdjustPrice = false we are returning the base price
n.AutoAdjustPrice = false
priceInfo, err = orch.PriceInfo(ethcommon.Address{})
priceInfo, err = orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(err)
assert.Equal(basePrice, big.NewRat(priceInfo.PricePerUnit, priceInfo.PixelsPerUnit))
}
Expand All @@ -1553,7 +1553,7 @@ func TestPriceInfo_GivenNilNode_ReturnsNilError(t *testing.T) {
orch := NewOrchestrator(n, nil)
orch.node = nil

priceInfo, err := orch.PriceInfo(ethcommon.Address{})
priceInfo, err := orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(t, err)
assert.Nil(t, priceInfo)
}
Expand All @@ -1563,7 +1563,7 @@ func TestPriceInfo_GivenNilRecipient_ReturnsNilError(t *testing.T) {
orch := NewOrchestrator(n, nil)
n.Recipient = nil

priceInfo, err := orch.PriceInfo(ethcommon.Address{})
priceInfo, err := orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(t, err)
assert.Nil(t, priceInfo)
}
Expand All @@ -1578,7 +1578,7 @@ func TestPriceInfo_TxMultiplierError_ReturnsError(t *testing.T) {
recipient.On("TxCostMultiplier", mock.Anything).Return(nil, expError)
orch := NewOrchestrator(n, nil)

priceInfo, err := orch.PriceInfo(ethcommon.Address{})
priceInfo, err := orch.PriceInfo(ethcommon.Address{}, "")
assert.Nil(t, priceInfo)
assert.EqualError(t, err, expError.Error())
}
Expand Down
5 changes: 0 additions & 5 deletions core/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,11 @@ func (orch *orchestrator) PriceInfo(sender ethcommon.Address, manifestID Manifes
func (orch *orchestrator) priceInfo(sender ethcommon.Address, manifestID ManifestID) (*big.Rat, error) {
basePrice := orch.node.GetBasePrice(sender.String())

glog.V(6).Infof("priceInfo")

// If there is already a fixed price for the given session, use this price
if manifestID != "" {
glog.V(6).Infof("manifestID = %v", manifestID)
if balances, ok := orch.node.Balances.balances[sender]; ok {
glog.V(6).Infof("sender = %v", sender)
fixedPrice := balances.FixedPrice(manifestID)
if fixedPrice != nil {
glog.V(6).Infof("##### Using fixed price=%v", fixedPrice)
return fixedPrice, nil
}
}
Expand Down
14 changes: 7 additions & 7 deletions server/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *stubOrchestrator) TicketParams(sender ethcommon.Address, priceInfo *net
return r.ticketParams, nil
}

func (r *stubOrchestrator) PriceInfo(sender ethcommon.Address) (*net.PriceInfo, error) {
func (r *stubOrchestrator) PriceInfo(sender ethcommon.Address, manifestID core.ManifestID) (*net.PriceInfo, error) {
return r.priceInfo, nil
}

Expand Down Expand Up @@ -1050,7 +1050,7 @@ func TestGetPriceInfo_NoWebhook_DefaultPriceError_ReturnsError(t *testing.T) {

orch.On("PriceInfo", mock.Anything).Return(nil, expErr)

p, err := getPriceInfo(orch, addr)
p, err := getPriceInfo(orch, addr, "")
assert.Nil(p)
assert.EqualError(err, expErr.Error())
}
Expand All @@ -1068,7 +1068,7 @@ func TestGetPriceInfo_NoWebhook_ReturnsDefaultPrice(t *testing.T) {

orch.On("PriceInfo", mock.Anything).Return(priceInfo, nil)

p, err := getPriceInfo(orch, addr)
p, err := getPriceInfo(orch, addr, "")
assert.Equal(p.PricePerUnit, int64(100))
assert.Equal(p.PixelsPerUnit, int64(30))
assert.Nil(err)
Expand All @@ -1092,7 +1092,7 @@ func TestGetPriceInfo_Webhook_NoCache_ReturnsDefaultPrice(t *testing.T) {

orch.On("PriceInfo", mock.Anything).Return(priceInfo, nil)

p, err := getPriceInfo(orch, addr)
p, err := getPriceInfo(orch, addr, "")
assert.Equal(p.PricePerUnit, int64(100))
assert.Equal(p.PixelsPerUnit, int64(30))
assert.Nil(err)
Expand All @@ -1118,7 +1118,7 @@ func TestGetPriceInfo_Webhook_Cache_WrongType_ReturnsDefaultPrice(t *testing.T)

orch.On("PriceInfo", mock.Anything).Return(priceInfo, nil)

p, err := getPriceInfo(orch, addr)
p, err := getPriceInfo(orch, addr, "")
assert.Equal(p.PricePerUnit, int64(100))
assert.Equal(p.PixelsPerUnit, int64(30))
assert.Nil(err)
Expand Down Expand Up @@ -1149,7 +1149,7 @@ func TestGetPriceInfo_Webhook_Cache_ReturnsCachePrice(t *testing.T) {

orch.On("PriceInfo", mock.Anything).Return(priceInfo, nil)

p, err := getPriceInfo(orch, addr)
p, err := getPriceInfo(orch, addr, "")
assert.Equal(p.PricePerUnit, int64(20))
assert.Equal(p.PixelsPerUnit, int64(19))
assert.Nil(err)
Expand Down Expand Up @@ -1323,7 +1323,7 @@ func (o *mockOrchestrator) TicketParams(sender ethcommon.Address, priceInfo *net
return nil, args.Error(1)
}

func (o *mockOrchestrator) PriceInfo(sender ethcommon.Address) (*net.PriceInfo, error) {
func (o *mockOrchestrator) PriceInfo(sender ethcommon.Address, manifestID core.ManifestID) (*net.PriceInfo, error) {
args := o.Called(sender)
if args.Get(0) != nil {
return args.Get(0).(*net.PriceInfo), args.Error(1)
Expand Down

0 comments on commit 0f621ac

Please sign in to comment.