Cycle through httpd.js ports to prevent CI failures in ZoteroPane tests

And remove unused XHR mock
This commit is contained in:
Dan Stillman 2023-07-01 22:58:38 -04:00
parent fb3466b392
commit 21c05bb8d7

View file

@ -143,21 +143,11 @@ describe("ZoteroPane", function() {
describe("#viewAttachment", function () { describe("#viewAttachment", function () {
Components.utils.import("resource://zotero-unit/httpd.js"); Components.utils.import("resource://zotero-unit/httpd.js");
var apiKey = Zotero.Utilities.randomString(24); var apiKey = Zotero.Utilities.randomString(24);
var port = 16213; var testServerPort = 16213;
var baseURL = `http://localhost:${port}/`; var maxTestServerPort = testServerPort + 10;
var server; var baseURL;
var responses = {};
var httpd; var httpd;
var setup = Zotero.Promise.coroutine(function* (options = {}) {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
function setResponse(response) {
setHTTPResponse(server, baseURL, response, responses);
}
async function downloadOnDemand() { async function downloadOnDemand() {
var item = new Zotero.Item("attachment"); var item = new Zotero.Item("attachment");
item.attachmentLinkMode = 'imported_file'; item.attachmentLinkMode = 'imported_file';
@ -212,12 +202,20 @@ describe("ZoteroPane", function() {
Zotero.HTTP.mock = sinon.FakeXMLHttpRequest; Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
}) })
beforeEach(function* () { beforeEach(function* () {
// Cycle through ports to prevent NS_ERROR_SOCKET_ADDRESS_IN_USE errors from server
// not always fully stopping in time
if (testServerPort < maxTestServerPort) {
testServerPort++;
}
else {
testServerPort--;
}
baseURL = `http://localhost:${testServerPort}/`;
Zotero.Prefs.set("api.url", baseURL); Zotero.Prefs.set("api.url", baseURL);
Zotero.Sync.Runner.apiKey = apiKey;
httpd = new HttpServer(); httpd = new HttpServer();
httpd.start(port); httpd.start(testServerPort);
Zotero.Sync.Runner.apiKey = apiKey;
yield Zotero.Users.setCurrentUserID(1); yield Zotero.Users.setCurrentUserID(1);
yield Zotero.Users.setCurrentUsername("testuser"); yield Zotero.Users.setCurrentUsername("testuser");
}) })
@ -231,14 +229,12 @@ describe("ZoteroPane", function() {
}); });
it("should download an attachment on-demand in as-needed mode", function* () { it("should download an attachment on-demand in as-needed mode", function* () {
yield setup();
Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true); Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);
yield downloadOnDemand(); yield downloadOnDemand();
}); });
// As noted in viewAttachment(), this is only necessary for files modified before 5.0.85 // As noted in viewAttachment(), this is only necessary for files modified before 5.0.85
it("should re-download a remotely modified attachment in as-needed mode", async function () { it("should re-download a remotely modified attachment in as-needed mode", async function () {
await setup();
Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true); Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);
var item = await importFileAttachment('test.txt'); var item = await importFileAttachment('test.txt');
@ -291,7 +287,6 @@ describe("ZoteroPane", function() {
}); });
it("should handle a 404 when re-downloading a remotely modified attachment in as-needed mode", async function () { it("should handle a 404 when re-downloading a remotely modified attachment in as-needed mode", async function () {
await setup();
Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true); Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);
var item = await importFileAttachment('test.txt'); var item = await importFileAttachment('test.txt');
@ -331,7 +326,6 @@ describe("ZoteroPane", function() {
}); });
it("should download an attachment on-demand in at-sync-time mode", function* () { it("should download an attachment on-demand in at-sync-time mode", function* () {
yield setup();
Zotero.Sync.Storage.Local.downloadOnSync(Zotero.Libraries.userLibraryID, true); Zotero.Sync.Storage.Local.downloadOnSync(Zotero.Libraries.userLibraryID, true);
yield downloadOnDemand(); yield downloadOnDemand();
}); });