github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/price_calculator_test.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package pingpong 19 20 import ( 21 "math/big" 22 "testing" 23 "time" 24 25 "github.com/mysteriumnetwork/node/datasize" 26 "github.com/mysteriumnetwork/node/market" 27 ) 28 29 func Test_isServiceFree(t *testing.T) { 30 tests := []struct { 31 name string 32 price *market.Price 33 want bool 34 }{ 35 { 36 name: "not free if only time payment is set", 37 price: market.NewPrice(10, 0), 38 want: false, 39 }, 40 { 41 name: "not free if only byte payment is set", 42 price: market.NewPrice(0, 10), 43 want: false, 44 }, 45 { 46 name: "not free if time + byte payment is set", 47 price: market.NewPrice(10, 10), 48 want: false, 49 }, 50 { 51 name: "free if empty", 52 price: market.NewPrice(0, 0), 53 want: true, 54 }, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 if got := tt.price.IsFree(); got != tt.want { 59 t.Errorf("isServiceFree() = %v, want %v", got, tt.want) 60 } 61 }) 62 } 63 } 64 65 func Test_CalculatePaymentAmount(t *testing.T) { 66 type args struct { 67 timePassed time.Duration 68 bytesTransferred DataTransferred 69 price *market.Price 70 } 71 tests := []struct { 72 name string 73 args args 74 want *big.Int 75 }{ 76 { 77 name: "returns zero on free service", 78 args: args{ 79 timePassed: time.Hour, 80 bytesTransferred: DataTransferred{ 81 Up: 100, Down: 100, 82 }, 83 price: market.NewPrice(0, 0), 84 }, 85 want: big.NewInt(0), 86 }, 87 { 88 name: "calculates time only", 89 args: args{ 90 timePassed: time.Hour, 91 bytesTransferred: DataTransferred{ 92 Up: 100, Down: 100, 93 }, 94 price: market.NewPrice(3000000, 0), 95 }, 96 want: big.NewInt(60 * 50000), 97 }, 98 { 99 name: "calculates bytes only", 100 args: args{ 101 timePassed: time.Hour, 102 bytesTransferred: DataTransferred{ 103 Up: datasize.GiB.Bytes() / 2, Down: datasize.GiB.Bytes() / 2, 104 }, 105 price: market.NewPrice(0, 7000000), 106 }, 107 want: big.NewInt(7000000), 108 }, 109 { 110 name: "calculates both", 111 args: args{ 112 timePassed: time.Hour, 113 bytesTransferred: DataTransferred{ 114 Up: datasize.GiB.Bytes() / 2, Down: datasize.GiB.Bytes() / 2, 115 }, 116 price: market.NewPrice(3000000, 7000000), 117 }, 118 // 7000000 is the price per gibibyte, 3000000 is the price per hour 119 want: big.NewInt(7000000 + 3000000), 120 }, 121 } 122 for _, tt := range tests { 123 t.Run(tt.name, func(t *testing.T) { 124 if got := CalculatePaymentAmount(tt.args.timePassed, tt.args.bytesTransferred, *tt.args.price); got.Cmp(tt.want) != 0 { 125 t.Errorf("CalculatePaymentAmount() = %v, want %v", got, tt.want) 126 } 127 }) 128 } 129 }