hosts.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. 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;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. var __read = (this && this.__read) || function (o, n) {
  38. var m = typeof Symbol === "function" && o[Symbol.iterator];
  39. if (!m) return o;
  40. var i = m.call(o), r, ar = [], e;
  41. try {
  42. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  43. }
  44. catch (error) { e = { error: error }; }
  45. finally {
  46. try {
  47. if (r && !r.done && (m = i["return"])) m.call(i);
  48. }
  49. finally { if (e) throw e.error; }
  50. }
  51. return ar;
  52. };
  53. var __spread = (this && this.__spread) || function () {
  54. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  55. return ar;
  56. };
  57. // eslint-disable-next-line import/newline-after-import
  58. import { MockApi } from '../api/index.mock';
  59. var mockApi = new MockApi();
  60. jest.mock('../api', function () { return mockApi; });
  61. // eslint-disable-next-line import/first
  62. import { Host, HostPool } from './hosts';
  63. function sleep(time) {
  64. if (time === void 0) { time = 100; }
  65. return new Promise(function (resolve, _) {
  66. setTimeout(resolve, time);
  67. });
  68. }
  69. describe('test hosts', function () {
  70. var getParams = ['accessKey', 'bucket', 'https'];
  71. test('getUp from api', function () { return __awaiter(void 0, void 0, void 0, function () {
  72. var hostPool, apiData, actual1, actual2, actual3;
  73. return __generator(this, function (_a) {
  74. switch (_a.label) {
  75. case 0:
  76. hostPool = new HostPool();
  77. return [4 /*yield*/, mockApi.getUpHosts()
  78. // 无冻结行为每次获取到的都是第一个
  79. ];
  80. case 1:
  81. apiData = _a.sent();
  82. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  83. case 2:
  84. actual1 = _a.sent();
  85. expect(actual1 === null || actual1 === void 0 ? void 0 : actual1.host).toStrictEqual(apiData.data.up.acc.main[0]);
  86. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  87. case 3:
  88. actual2 = _a.sent();
  89. expect(actual2 === null || actual2 === void 0 ? void 0 : actual2.host).toStrictEqual(apiData.data.up.acc.main[0]);
  90. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  91. case 4:
  92. actual3 = _a.sent();
  93. expect(actual3 === null || actual3 === void 0 ? void 0 : actual3.host).toStrictEqual(apiData.data.up.acc.main[0]);
  94. return [2 /*return*/];
  95. }
  96. });
  97. }); });
  98. test('getUp from config', function () { return __awaiter(void 0, void 0, void 0, function () {
  99. var hostPool, actual1, actual2, actual3;
  100. return __generator(this, function (_a) {
  101. switch (_a.label) {
  102. case 0:
  103. hostPool = new HostPool([
  104. 'host-1',
  105. 'host-2'
  106. ]);
  107. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  108. case 1:
  109. actual1 = _a.sent();
  110. expect(actual1).toStrictEqual(new Host('host-1', 'https'));
  111. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  112. case 2:
  113. actual2 = _a.sent();
  114. expect(actual2).toStrictEqual(new Host('host-1', 'https'));
  115. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  116. case 3:
  117. actual3 = _a.sent();
  118. expect(actual3).toStrictEqual(new Host('host-1', 'https'));
  119. return [2 /*return*/];
  120. }
  121. });
  122. }); });
  123. test('freeze & unfreeze', function () { return __awaiter(void 0, void 0, void 0, function () {
  124. var hostPool, host1, host2, actual1, actual2, actual3, actual4, actual5;
  125. return __generator(this, function (_a) {
  126. switch (_a.label) {
  127. case 0:
  128. hostPool = new HostPool([
  129. 'host-1',
  130. 'host-2'
  131. ]);
  132. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  133. case 1:
  134. host1 = _a.sent();
  135. expect(host1).toStrictEqual(new Host('host-1', 'https'));
  136. // eslint-disable-next-line no-unused-expressions
  137. host1 === null || host1 === void 0 ? void 0 : host1.freeze();
  138. return [4 /*yield*/, sleep()
  139. // 自动切换到了下一个可用的 host-2
  140. ];
  141. case 2:
  142. _a.sent();
  143. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  144. case 3:
  145. host2 = _a.sent();
  146. expect(host2).toStrictEqual(new Host('host-2', 'https'));
  147. // eslint-disable-next-line no-unused-expressions
  148. host2 === null || host2 === void 0 ? void 0 : host2.freeze();
  149. return [4 /*yield*/, sleep()
  150. // 以下是都被冻结情况的测试
  151. // 全部都冻结了,拿到的应该是离解冻时间最近的一个
  152. ];
  153. case 4:
  154. _a.sent();
  155. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  156. case 5:
  157. actual1 = _a.sent();
  158. expect(actual1).toStrictEqual(new Host('host-1', 'https'));
  159. // eslint-disable-next-line no-unused-expressions
  160. host1 === null || host1 === void 0 ? void 0 : host1.freeze(); // 已经冻结的再次冻结相当于更新解冻时间
  161. return [4 /*yield*/, sleep()
  162. // 因为 host-1 刚更新过冻结时间,所以这个时候解冻时间优先的应该是 host-2
  163. ];
  164. case 6:
  165. _a.sent();
  166. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  167. case 7:
  168. actual2 = _a.sent();
  169. expect(actual2).toStrictEqual(new Host('host-2', 'https'));
  170. return [4 /*yield*/, sleep()
  171. // 测试解冻 host-2,拿到的应该是 host-2
  172. // eslint-disable-next-line no-unused-expressions
  173. ];
  174. case 8:
  175. _a.sent();
  176. // 测试解冻 host-2,拿到的应该是 host-2
  177. // eslint-disable-next-line no-unused-expressions
  178. host2 === null || host2 === void 0 ? void 0 : host2.unfreeze();
  179. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  180. case 9:
  181. actual3 = _a.sent();
  182. expect(actual3).toStrictEqual(new Host('host-2', 'https'));
  183. // eslint-disable-next-line no-unused-expressions
  184. host2 === null || host2 === void 0 ? void 0 : host2.freeze(); // 测试完再冻结住
  185. return [4 /*yield*/, sleep()
  186. // 本来优先的现在应该是 host-1
  187. // 测试 host-2 冻结时间设置为 0,应该获取到 host-2
  188. // eslint-disable-next-line no-unused-expressions
  189. ];
  190. case 10:
  191. _a.sent();
  192. // 本来优先的现在应该是 host-1
  193. // 测试 host-2 冻结时间设置为 0,应该获取到 host-2
  194. // eslint-disable-next-line no-unused-expressions
  195. host2 === null || host2 === void 0 ? void 0 : host2.freeze(0);
  196. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  197. case 11:
  198. actual4 = _a.sent();
  199. expect(actual4).toStrictEqual(new Host('host-2', 'https'));
  200. // eslint-disable-next-line no-unused-expressions
  201. host2 === null || host2 === void 0 ? void 0 : host2.freeze();
  202. return [4 /*yield*/, sleep()
  203. // 测试自定义冻结时间
  204. // eslint-disable-next-line no-unused-expressions
  205. ];
  206. case 12:
  207. _a.sent();
  208. // 测试自定义冻结时间
  209. // eslint-disable-next-line no-unused-expressions
  210. host1 === null || host1 === void 0 ? void 0 : host1.freeze(200);
  211. // eslint-disable-next-line no-unused-expressions
  212. host2 === null || host2 === void 0 ? void 0 : host2.freeze(100);
  213. return [4 /*yield*/, hostPool.getUp.apply(hostPool, __spread(getParams))];
  214. case 13:
  215. actual5 = _a.sent();
  216. expect(actual5).toStrictEqual(new Host('host-2', 'https'));
  217. return [2 /*return*/];
  218. }
  219. });
  220. }); });
  221. });
  222. //# sourceMappingURL=hosts.test.js.map