Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix download accounting test #217

Merged
merged 7 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions tests/cli_tests/zboxcli_download_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ func TestFileDownloadTokenMovement(t *testing.T) {
require.Nil(t, err, "Could not get download cost", strings.Join(output, "\n"))
require.Len(t, output, 1)

expectedDownloadCostInZCN, err := strconv.ParseFloat(strings.Fields(output[0])[0], 64)
expectedDownloadCost, err := strconv.ParseFloat(strings.Fields(output[0])[0], 64)
require.Nil(t, err, "Cost couldn't be parsed to float", strings.Join(output, "\n"))

unit := strings.Fields(output[0])[1]
expectedDownloadCostInZCN = unitToZCN(expectedDownloadCostInZCN, unit)
expectedDownloadCostInZCN := unitToZCN(expectedDownloadCost, unit)

// Download the file
output, err = downloadFile(t, configPath, createParams(map[string]interface{}{
Expand All @@ -208,6 +208,10 @@ func TestFileDownloadTokenMovement(t *testing.T) {
require.Len(t, output, 2)
require.Equal(t, "Status completed callback. Type = application/octet-stream. Name = "+filename, output[1])

// Wait for blobber to redeem read-tokens
// Blobber runs worker in the interval of usually 10 seconds.
time.Sleep(time.Second * 20)

// Read pool after download
output, err = readPoolInfo(t, configPath, allocationID)
require.Nil(t, err, "Error fetching read pool", strings.Join(output, "\n"))
Expand All @@ -217,8 +221,9 @@ func TestFileDownloadTokenMovement(t *testing.T) {
err = json.Unmarshal([]byte(output[0]), &finalReadPool)
require.Nil(t, err, "Error unmarshalling read pool", strings.Join(output, "\n"))

expectedRPBalance := 0.4*1e10 - expectedDownloadCostInZCN*1e10
require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Id)
require.Less(t, intToZCN(finalReadPool[0].Balance), 0.4)
require.InEpsilon(t, float64(finalReadPool[0].Balance), expectedRPBalance, epsilon)
require.IsType(t, int64(1), finalReadPool[0].ExpireAt)
require.Equal(t, allocationID, finalReadPool[0].AllocationId)
require.Equal(t, len(initialReadPool[0].Blobber), len(finalReadPool[0].Blobber))
Expand All @@ -227,11 +232,7 @@ func TestFileDownloadTokenMovement(t *testing.T) {
for i := 0; i < len(finalReadPool[0].Blobber); i++ {
require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Blobber[i].BlobberID)
require.IsType(t, int64(1), finalReadPool[0].Blobber[i].Balance)

// amount deducted
diff := intToZCN(initialReadPool[0].Blobber[i].Balance) - intToZCN(finalReadPool[0].Blobber[i].Balance)
t.Logf("blobber [%v] read pool was deducted by [%v]", i, diff)
require.InEpsilon(t, expectedDownloadCostInZCN, diff, epsilon, "blobber [%v] read pool was deducted by [%v] rather than the expected [%v]", i, diff, expectedDownloadCostInZCN)
require.Greater(t, initialReadPool[0].Blobber[i].Balance, finalReadPool[0].Blobber[i].Balance)
}
})
}
Expand Down Expand Up @@ -285,9 +286,6 @@ func unitToZCN(unitCost float64, unit string) float64 {
case "mZCN", "mzcn":
unitCost /= 1e3
return unitCost
case "ZCN", "zcn":
unitCost /= 1e0
return unitCost
}
return unitCost
}
42 changes: 26 additions & 16 deletions tests/cli_tests/zboxcli_share_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,12 +1049,12 @@ func TestShareFile(t *testing.T) {
require.Nil(t, err, "Could not get download cost", strings.Join(output, "\n"))
require.Len(t, output, 1)

expectedDownloadCostInZCN, err := strconv.ParseFloat(strings.Fields(output[0])[0], 64)
expectedDownloadCost, err := strconv.ParseFloat(strings.Fields(output[0])[0], 64)
require.Nil(t, err, "Cost couldn't be parsed to float", strings.Join(output, "\n"))

unit := strings.Fields(output[0])[1]
expectedDownloadCostInZCN = unitToZCN(expectedDownloadCostInZCN, unit) * 1e10
t.Logf("Download cost: %v", expectedDownloadCostInZCN)
expectedDownloadCostInSas := unitToZCN(expectedDownloadCost, unit) * 1e10
t.Logf("Download cost: %v sas", expectedDownloadCostInSas)

// Download the file (delete local copy first)
os.Remove(file)
Expand All @@ -1079,8 +1079,10 @@ func TestShareFile(t *testing.T) {
require.Nil(t, err, "Error unmarshalling read pool", strings.Join(output, "\n"))
require.NotEmpty(t, finalReadPool)

expectedRPBalance := 0.4*1e10 - expectedDownloadCostInSas

require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Id)
require.Equal(t, 0.4*1e10, float64(finalReadPool[0].Balance))
require.InEpsilon(t, expectedRPBalance, float64(finalReadPool[0].Balance), epsilon)
require.Equal(t, allocationID, finalReadPool[0].AllocationId)
require.Equal(t, len(initialReadPool[0].Blobber), len(finalReadPool[0].Blobber))
require.True(t, finalReadPool[0].Locked)
Expand All @@ -1089,7 +1091,7 @@ func TestShareFile(t *testing.T) {
for i := 0; i < len(finalReadPool[0].Blobber); i++ {
require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Blobber[i].BlobberID)
t.Logf("Blobber [%v] balance is [%v]", i, initialReadPool[0].Blobber[i].Balance)
require.Equal(t, initialReadPool[0].Blobber[i].Balance, finalReadPool[0].Blobber[i].Balance)
require.Greater(t, initialReadPool[0].Blobber[i].Balance, finalReadPool[0].Blobber[i].Balance)
}
})

Expand Down Expand Up @@ -1163,12 +1165,12 @@ func TestShareFile(t *testing.T) {
require.Nil(t, err, "Could not get download cost", strings.Join(output, "\n"))
require.Len(t, output, 1)

expectedDownloadCostInZCN, err := strconv.ParseFloat(strings.Fields(output[0])[0], 64)
expectedDownloadCost, err := strconv.ParseFloat(strings.Fields(output[0])[0], 64)
require.Nil(t, err, "Cost couldn't be parsed to float", strings.Join(output, "\n"))

unit := strings.Fields(output[0])[1]
expectedDownloadCostInZCN = unitToZCN(expectedDownloadCostInZCN, unit) * 1e10
t.Logf("Download cost: %v", expectedDownloadCostInZCN)
expectedDownloadCostInSas := unitToZCN(expectedDownloadCost, unit) * 1e10
t.Logf("Download cost: %v sas", expectedDownloadCostInSas)

// Download the file (delete local copy first)
os.Remove(file)
Expand All @@ -1183,6 +1185,9 @@ func TestShareFile(t *testing.T) {
require.Equal(t, "Status completed callback. Type = application/octet-stream. Name = "+filepath.Base(file), output[1],
"download file - Unexpected output", strings.Join(output, "\n"))

// Wait for blobber to redeem read-tokens
// Blobber runs worker in the interval of usually 10 seconds.
time.Sleep(time.Second * 20)
// Read pool after download
output, err = readPoolInfo(t, configPath, allocationID)
require.Nil(t, err, "Error fetching read pool", strings.Join(output, "\n"))
Expand All @@ -1193,8 +1198,9 @@ func TestShareFile(t *testing.T) {
require.Nil(t, err, "Error unmarshalling read pool", strings.Join(output, "\n"))
require.NotEmpty(t, finalReadPool)

expectedRPBalance := 0.4*1e10 - expectedDownloadCostInSas
require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Id)
require.Equal(t, 0.4*1e10, float64(finalReadPool[0].Balance))
require.InEpsilon(t, expectedRPBalance, float64(finalReadPool[0].Balance), epsilon)
require.Equal(t, allocationID, finalReadPool[0].AllocationId)
require.Equal(t, len(initialReadPool[0].Blobber), len(finalReadPool[0].Blobber))
require.True(t, finalReadPool[0].Locked)
Expand All @@ -1203,7 +1209,7 @@ func TestShareFile(t *testing.T) {
for i := 0; i < len(finalReadPool[0].Blobber); i++ {
require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Blobber[i].BlobberID)
t.Logf("Blobber [%v] balance is [%v]", i, initialReadPool[0].Blobber[i].Balance)
require.Equal(t, initialReadPool[0].Blobber[i].Balance, finalReadPool[0].Blobber[i].Balance)
require.Greater(t, initialReadPool[0].Blobber[i].Balance, finalReadPool[0].Blobber[i].Balance)
}
})

Expand Down Expand Up @@ -1323,6 +1329,9 @@ func TestShareFile(t *testing.T) {
require.Equal(t, "Status completed callback. Type = application/octet-stream. Name = "+filepath.Base(file), output[1],
"download file - Unexpected output", strings.Join(output, "\n"))

// Wait for blobber to redeem read-tokens
// Blobber runs worker in the interval of usually 10 seconds.
time.Sleep(time.Second * 20)
// Read pool after download
output, err = readPoolInfoWithwallet(t, receiverWallet, configPath, allocationID)
require.Nil(t, err, "Error fetching read pool", strings.Join(output, "\n"))
Expand All @@ -1342,11 +1351,10 @@ func TestShareFile(t *testing.T) {

for i := 0; i < len(finalReadPool[0].Blobber); i++ {
require.Regexp(t, regexp.MustCompile("([a-f0-9]{64})"), finalReadPool[0].Blobber[i].BlobberID)

// amount deducted
diff := initialReadPool[0].Blobber[i].Balance - finalReadPool[0].Blobber[i].Balance
t.Logf("blobber [%v] read pool was deducted by [%v]", i, diff)
require.InEpsilon(t, expectedDownloadCostInZCN, diff, epsilon, "blobber [%v] read pool was deducted by [%v] rather than the expected [%v]", i, diff, expectedDownloadCostInZCN)
initialBal := initialReadPool[0].Blobber[i].Balance
finalBal := finalReadPool[0].Blobber[i].Balance
require.Greater(t, initialBal, finalBal, "Blobber [%v] initial balance: [%v] and final balance: [%v]",
i, initialBal, finalBal)
}
})

Expand Down Expand Up @@ -1480,7 +1488,9 @@ func TestShareFile(t *testing.T) {
// amount deducted
diff := initialReadPool[0].Blobber[i].Balance - finalReadPool[0].Blobber[i].Balance
t.Logf("blobber [%v] read pool was deducted by [%v]", i, diff)
require.InEpsilon(t, expectedDownloadCostInZCN, diff, epsilon, "blobber [%v] read pool was deducted by [%v] rather than the expected [%v]", i, diff, expectedDownloadCostInZCN)
initialBalance := initialReadPool[0].Blobber[i].Balance
finalBalance := finalReadPool[0].Blobber[i].Balance
require.Greater(t, initialBalance, finalBalance, "blobber [%v] initial balance was [%v] and final balance is [%v]", i, initialBalance, finalBalance)
}
})
}
Expand Down