(t *testing.T)
| 110 | } |
| 111 | |
| 112 | func TestUserPanel_UpdateUsageQueue(t *testing.T) { |
| 113 | var tmpDB, _ = ioutil.TempFile("", "ck_user_info") |
| 114 | defer os.Remove(tmpDB.Name()) |
| 115 | mgr, err := usermanager.MakeLocalManager(tmpDB.Name(), mockWorldState) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | panel := MakeUserPanel(mgr) |
| 120 | |
| 121 | t.Run("normal update", func(t *testing.T) { |
| 122 | _ = mgr.WriteUserInfo(validUserInfo) |
| 123 | |
| 124 | user, err := panel.GetUser(validUserInfo.UID) |
| 125 | if err != nil { |
| 126 | t.Error(err) |
| 127 | } |
| 128 | |
| 129 | user.valve.AddTx(1) |
| 130 | user.valve.AddRx(2) |
| 131 | panel.updateUsageQueue() |
| 132 | err = panel.commitUpdate() |
| 133 | if err != nil { |
| 134 | t.Error(err) |
| 135 | } |
| 136 | |
| 137 | if user.valve.GetRx() != 0 || user.valve.GetTx() != 0 { |
| 138 | t.Error("rx and tx stats are not cleared") |
| 139 | } |
| 140 | |
| 141 | updatedUinfo, _ := mgr.GetUserInfo(validUserInfo.UID) |
| 142 | if *updatedUinfo.DownCredit != *validUserInfo.DownCredit-1 { |
| 143 | t.Error("down credit incorrect update") |
| 144 | } |
| 145 | if *updatedUinfo.UpCredit != *validUserInfo.UpCredit-2 { |
| 146 | t.Error("up credit incorrect update") |
| 147 | } |
| 148 | |
| 149 | // another update |
| 150 | user.valve.AddTx(3) |
| 151 | user.valve.AddRx(4) |
| 152 | panel.updateUsageQueue() |
| 153 | err = panel.commitUpdate() |
| 154 | if err != nil { |
| 155 | t.Error(err) |
| 156 | } |
| 157 | |
| 158 | updatedUinfo, _ = mgr.GetUserInfo(validUserInfo.UID) |
| 159 | if *updatedUinfo.DownCredit != *validUserInfo.DownCredit-(1+3) { |
| 160 | t.Error("down credit incorrect update") |
| 161 | } |
| 162 | if *updatedUinfo.UpCredit != *validUserInfo.UpCredit-(2+4) { |
| 163 | t.Error("up credit incorrect update") |
| 164 | } |
| 165 | }) |
| 166 | t.Run("terminating update", func(t *testing.T) { |
| 167 | _ = mgr.WriteUserInfo(validUserInfo) |
| 168 | |
| 169 | user, err := panel.GetUser(validUserInfo.UID) |
nothing calls this directly
no test coverage detected