hosts.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
  3. var _iterator = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol/iterator"));
  4. var _symbol = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol"));
  5. var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
  6. var _index = require("../api/index.mock");
  7. var _hosts = require("./hosts");
  8. var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  9. function adopt(value) {
  10. return value instanceof P ? value : new P(function (resolve) {
  11. resolve(value);
  12. });
  13. }
  14. return new (P || (P = _promise["default"]))(function (resolve, reject) {
  15. function fulfilled(value) {
  16. try {
  17. step(generator.next(value));
  18. } catch (e) {
  19. reject(e);
  20. }
  21. }
  22. function rejected(value) {
  23. try {
  24. step(generator["throw"](value));
  25. } catch (e) {
  26. reject(e);
  27. }
  28. }
  29. function step(result) {
  30. result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
  31. }
  32. step((generator = generator.apply(thisArg, _arguments || [])).next());
  33. });
  34. };
  35. var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
  36. var _ = {
  37. label: 0,
  38. sent: function sent() {
  39. if (t[0] & 1) throw t[1];
  40. return t[1];
  41. },
  42. trys: [],
  43. ops: []
  44. },
  45. f,
  46. y,
  47. t,
  48. g;
  49. return g = {
  50. next: verb(0),
  51. "throw": verb(1),
  52. "return": verb(2)
  53. }, typeof _symbol["default"] === "function" && (g[_iterator["default"]] = function () {
  54. return this;
  55. }), g;
  56. function verb(n) {
  57. return function (v) {
  58. return step([n, v]);
  59. };
  60. }
  61. function step(op) {
  62. if (f) throw new TypeError("Generator is already executing.");
  63. while (_) {
  64. try {
  65. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  66. if (y = 0, t) op = [op[0] & 2, t.value];
  67. switch (op[0]) {
  68. case 0:
  69. case 1:
  70. t = op;
  71. break;
  72. case 4:
  73. _.label++;
  74. return {
  75. value: op[1],
  76. done: false
  77. };
  78. case 5:
  79. _.label++;
  80. y = op[1];
  81. op = [0];
  82. continue;
  83. case 7:
  84. op = _.ops.pop();
  85. _.trys.pop();
  86. continue;
  87. default:
  88. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
  89. _ = 0;
  90. continue;
  91. }
  92. if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
  93. _.label = op[1];
  94. break;
  95. }
  96. if (op[0] === 6 && _.label < t[1]) {
  97. _.label = t[1];
  98. t = op;
  99. break;
  100. }
  101. if (t && _.label < t[2]) {
  102. _.label = t[2];
  103. _.ops.push(op);
  104. break;
  105. }
  106. if (t[2]) _.ops.pop();
  107. _.trys.pop();
  108. continue;
  109. }
  110. op = body.call(thisArg, _);
  111. } catch (e) {
  112. op = [6, e];
  113. y = 0;
  114. } finally {
  115. f = t = 0;
  116. }
  117. }
  118. if (op[0] & 5) throw op[1];
  119. return {
  120. value: op[0] ? op[1] : void 0,
  121. done: true
  122. };
  123. }
  124. };
  125. var __read = void 0 && (void 0).__read || function (o, n) {
  126. var m = typeof _symbol["default"] === "function" && o[_iterator["default"]];
  127. if (!m) return o;
  128. var i = m.call(o),
  129. r,
  130. ar = [],
  131. e;
  132. try {
  133. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
  134. ar.push(r.value);
  135. }
  136. } catch (error) {
  137. e = {
  138. error: error
  139. };
  140. } finally {
  141. try {
  142. if (r && !r.done && (m = i["return"])) m.call(i);
  143. } finally {
  144. if (e) throw e.error;
  145. }
  146. }
  147. return ar;
  148. };
  149. var __spread = void 0 && (void 0).__spread || function () {
  150. for (var ar = [], i = 0; i < arguments.length; i++) {
  151. ar = ar.concat(__read(arguments[i]));
  152. }
  153. return ar;
  154. }; // eslint-disable-next-line import/newline-after-import
  155. var mockApi = new _index.MockApi();
  156. jest.mock('../api', function () {
  157. return mockApi;
  158. }); // eslint-disable-next-line import/first
  159. function sleep(time) {
  160. if (time === void 0) {
  161. time = 100;
  162. }
  163. return new _promise["default"](function (resolve, _) {
  164. setTimeout(resolve, time);
  165. });
  166. }
  167. describe('test hosts', function () {
  168. var getParams = ['accessKey', 'bucket', 'https'];
  169. test('getUp from api', function () {
  170. return __awaiter(void 0, void 0, void 0, function () {
  171. var hostPool, apiData, actual1, actual2, actual3;
  172. return __generator(this, function (_a) {
  173. switch (_a.label) {
  174. case 0:
  175. hostPool = new _hosts.HostPool();
  176. return [4
  177. /*yield*/
  178. , mockApi.getUpHosts() // 无冻结行为每次获取到的都是第一个
  179. ];
  180. case 1:
  181. apiData = _a.sent();
  182. return [4
  183. /*yield*/
  184. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  185. case 2:
  186. actual1 = _a.sent();
  187. expect(actual1 === null || actual1 === void 0 ? void 0 : actual1.host).toStrictEqual(apiData.data.up.acc.main[0]);
  188. return [4
  189. /*yield*/
  190. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  191. case 3:
  192. actual2 = _a.sent();
  193. expect(actual2 === null || actual2 === void 0 ? void 0 : actual2.host).toStrictEqual(apiData.data.up.acc.main[0]);
  194. return [4
  195. /*yield*/
  196. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  197. case 4:
  198. actual3 = _a.sent();
  199. expect(actual3 === null || actual3 === void 0 ? void 0 : actual3.host).toStrictEqual(apiData.data.up.acc.main[0]);
  200. return [2
  201. /*return*/
  202. ];
  203. }
  204. });
  205. });
  206. });
  207. test('getUp from config', function () {
  208. return __awaiter(void 0, void 0, void 0, function () {
  209. var hostPool, actual1, actual2, actual3;
  210. return __generator(this, function (_a) {
  211. switch (_a.label) {
  212. case 0:
  213. hostPool = new _hosts.HostPool(['host-1', 'host-2']);
  214. return [4
  215. /*yield*/
  216. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  217. case 1:
  218. actual1 = _a.sent();
  219. expect(actual1).toStrictEqual(new _hosts.Host('host-1', 'https'));
  220. return [4
  221. /*yield*/
  222. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  223. case 2:
  224. actual2 = _a.sent();
  225. expect(actual2).toStrictEqual(new _hosts.Host('host-1', 'https'));
  226. return [4
  227. /*yield*/
  228. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  229. case 3:
  230. actual3 = _a.sent();
  231. expect(actual3).toStrictEqual(new _hosts.Host('host-1', 'https'));
  232. return [2
  233. /*return*/
  234. ];
  235. }
  236. });
  237. });
  238. });
  239. test('freeze & unfreeze', function () {
  240. return __awaiter(void 0, void 0, void 0, function () {
  241. var hostPool, host1, host2, actual1, actual2, actual3, actual4, actual5;
  242. return __generator(this, function (_a) {
  243. switch (_a.label) {
  244. case 0:
  245. hostPool = new _hosts.HostPool(['host-1', 'host-2']);
  246. return [4
  247. /*yield*/
  248. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  249. case 1:
  250. host1 = _a.sent();
  251. expect(host1).toStrictEqual(new _hosts.Host('host-1', 'https')); // eslint-disable-next-line no-unused-expressions
  252. host1 === null || host1 === void 0 ? void 0 : host1.freeze();
  253. return [4
  254. /*yield*/
  255. , sleep() // 自动切换到了下一个可用的 host-2
  256. ];
  257. case 2:
  258. _a.sent();
  259. return [4
  260. /*yield*/
  261. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  262. case 3:
  263. host2 = _a.sent();
  264. expect(host2).toStrictEqual(new _hosts.Host('host-2', 'https')); // eslint-disable-next-line no-unused-expressions
  265. host2 === null || host2 === void 0 ? void 0 : host2.freeze();
  266. return [4
  267. /*yield*/
  268. , sleep() // 以下是都被冻结情况的测试
  269. // 全部都冻结了,拿到的应该是离解冻时间最近的一个
  270. ];
  271. case 4:
  272. _a.sent();
  273. return [4
  274. /*yield*/
  275. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  276. case 5:
  277. actual1 = _a.sent();
  278. expect(actual1).toStrictEqual(new _hosts.Host('host-1', 'https')); // eslint-disable-next-line no-unused-expressions
  279. host1 === null || host1 === void 0 ? void 0 : host1.freeze(); // 已经冻结的再次冻结相当于更新解冻时间
  280. return [4
  281. /*yield*/
  282. , sleep() // 因为 host-1 刚更新过冻结时间,所以这个时候解冻时间优先的应该是 host-2
  283. ];
  284. case 6:
  285. _a.sent();
  286. return [4
  287. /*yield*/
  288. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  289. case 7:
  290. actual2 = _a.sent();
  291. expect(actual2).toStrictEqual(new _hosts.Host('host-2', 'https'));
  292. return [4
  293. /*yield*/
  294. , sleep() // 测试解冻 host-2,拿到的应该是 host-2
  295. // eslint-disable-next-line no-unused-expressions
  296. ];
  297. case 8:
  298. _a.sent(); // 测试解冻 host-2,拿到的应该是 host-2
  299. // eslint-disable-next-line no-unused-expressions
  300. host2 === null || host2 === void 0 ? void 0 : host2.unfreeze();
  301. return [4
  302. /*yield*/
  303. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  304. case 9:
  305. actual3 = _a.sent();
  306. expect(actual3).toStrictEqual(new _hosts.Host('host-2', 'https')); // eslint-disable-next-line no-unused-expressions
  307. host2 === null || host2 === void 0 ? void 0 : host2.freeze(); // 测试完再冻结住
  308. return [4
  309. /*yield*/
  310. , sleep() // 本来优先的现在应该是 host-1
  311. // 测试 host-2 冻结时间设置为 0,应该获取到 host-2
  312. // eslint-disable-next-line no-unused-expressions
  313. ];
  314. case 10:
  315. _a.sent(); // 本来优先的现在应该是 host-1
  316. // 测试 host-2 冻结时间设置为 0,应该获取到 host-2
  317. // eslint-disable-next-line no-unused-expressions
  318. host2 === null || host2 === void 0 ? void 0 : host2.freeze(0);
  319. return [4
  320. /*yield*/
  321. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  322. case 11:
  323. actual4 = _a.sent();
  324. expect(actual4).toStrictEqual(new _hosts.Host('host-2', 'https')); // eslint-disable-next-line no-unused-expressions
  325. host2 === null || host2 === void 0 ? void 0 : host2.freeze();
  326. return [4
  327. /*yield*/
  328. , sleep() // 测试自定义冻结时间
  329. // eslint-disable-next-line no-unused-expressions
  330. ];
  331. case 12:
  332. _a.sent(); // 测试自定义冻结时间
  333. // eslint-disable-next-line no-unused-expressions
  334. host1 === null || host1 === void 0 ? void 0 : host1.freeze(200); // eslint-disable-next-line no-unused-expressions
  335. host2 === null || host2 === void 0 ? void 0 : host2.freeze(100);
  336. return [4
  337. /*yield*/
  338. , hostPool.getUp.apply(hostPool, __spread(getParams))];
  339. case 13:
  340. actual5 = _a.sent();
  341. expect(actual5).toStrictEqual(new _hosts.Host('host-2', 'https'));
  342. return [2
  343. /*return*/
  344. ];
  345. }
  346. });
  347. });
  348. });
  349. });