Skip to content

Commit a410a87

Browse files
committed
Add unit test
1 parent 1cc0a2b commit a410a87

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/database/import.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type ChunkedData = {
3030
export default class DatabaseImporter {
3131
private client: Client;
3232
private limit: pLimit.Limit;
33+
nonFatalRetryTimeout = 1000; // To be overriden in tests
3334

3435
constructor(
3536
private dbUrl: URL,
@@ -140,7 +141,7 @@ export default class DatabaseImporter {
140141
err.original.code === "ETIMEDOUT";
141142
if (isTimeoutErr) {
142143
// RTDB connection timeouts are transient and can be retried
143-
await new Promise((res) => setTimeout(res, 1000));
144+
await new Promise((res) => setTimeout(res, this.nonFatalRetryTimeout));
144145
return await doRequest();
145146
}
146147
throw err;

src/test/database/import.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { expect } from "chai";
55

66
import DatabaseImporter from "../../database/import";
77
import { FirebaseError } from "../../error";
8+
import { FetchError } from "node-fetch";
89

910
const dbUrl = new URL("https://test-db.firebaseio.com/foo");
1011
const chunkSize = 1024 * 1024 * 10;
@@ -112,4 +113,33 @@ describe("DatabaseImporter", () => {
112113
/Importing is only allowed for an empty location./
113114
);
114115
});
116+
117+
it("retries non-fatal connection timeout error", async () => {
118+
const timeoutErr = new FetchError("connect ETIMEDOUT", "system");
119+
timeoutErr.code = "ETIMEDOUT";
120+
121+
nock("https://test-db.firebaseio.com").get("/foo.json?shallow=true").reply(200);
122+
nock("https://test-db.firebaseio.com")
123+
.put("/foo/a.json", "100")
124+
.once()
125+
.replyWithError(timeoutErr);
126+
nock("https://test-db.firebaseio.com").put("/foo/a.json", "100").once().reply(200);
127+
nock("https://test-db.firebaseio.com")
128+
.put("/foo/b.json", JSON.stringify([true, "bar", { f: { g: 0, h: 1 }, i: "baz" }]))
129+
.reply(200);
130+
131+
const importer = new DatabaseImporter(
132+
dbUrl,
133+
DATA_STREAM,
134+
/* importPath= */ "/",
135+
chunkSize,
136+
concurrencyLimit
137+
);
138+
importer.nonFatalRetryTimeout = 0;
139+
140+
const responses = await importer.execute();
141+
142+
expect(responses).to.have.length(2);
143+
expect(nock.isDone()).to.be.true;
144+
});
115145
});

0 commit comments

Comments
 (0)