Skip to content

Commit

Permalink
Merge pull request #991 from golemfactory/feature/JST-992/make-end-me…
Browse files Browse the repository at this point in the history
…thods-indempotent

feat: make `glm.disconnect` `rentalPool.drainAndClear` and `rental.stopAndFinalize` idempotent
  • Loading branch information
SewerynKras authored Jun 28, 2024
2 parents 1021373 + 8f7ba3f commit 9abfafb
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 199 deletions.
74 changes: 72 additions & 2 deletions src/golem-network/golem-network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { RentalModuleImpl, ResourceRental, ResourceRentalPool } from "../resourc
import { DraftOfferProposalPool, MarketModuleImpl, OfferProposal } from "../market";
import { NetworkModuleImpl } from "../network";
import { Allocation, PaymentModuleImpl } from "../payment";
import { YagnaApi } from "../shared/utils";
import { YagnaApi, sleep } from "../shared/utils";
import { MarketApiAdapter, PaymentApiAdapter } from "../shared/yagna";
import { ActivityApiAdapter } from "../shared/yagna/adapters/activity-api-adapter";
import { GolemNetwork, MarketOrderSpec } from "./golem-network";
import { _, instance, mock, reset, verify, when } from "@johanblumenberg/ts-mockito";
import { _, instance, mock, reset, spy, verify, when } from "@johanblumenberg/ts-mockito";
import { GftpStorageProvider } from "../shared/storage";

const order: MarketOrderSpec = Object.freeze({
Expand Down Expand Up @@ -187,4 +187,74 @@ describe("Golem Network", () => {
verify(mockPayment.releaseAllocation(allocation)).never();
});
});
describe("disconnect()", () => {
it("reuses the same promise if called multiple times", async () => {
const glm = getGolemNetwork();
await glm.connect();
const glmSpy = spy(glm);
when(glmSpy["startDisconnect"]()).thenResolve();
expect(glm["disconnectPromise"]).toBeUndefined();
const promise1 = glm.disconnect();
const promise2 = glm.disconnect();
const promise3 = glm.disconnect();
expect(glm["disconnectPromise"]).toBeDefined();
await Promise.all([promise1, promise2, promise3]);
verify(glmSpy["startDisconnect"]()).once();
expect(glm["disconnectPromise"]).toBeUndefined();
});
it("stops any oneOf() rentals that are in the process of being created", async () => {
const allocation = instance(mock(Allocation));
when(mockPayment.createAllocation(_)).thenResolve(allocation);
when(mockPayment.releaseAllocation(allocation)).thenResolve();

// simulate some operation taking a long time
when(mockPayment.createAllocation(_)).thenCall(async () => {
await sleep(100, true);
return allocation;
});

const glm = getGolemNetwork();
await glm.connect();

expect.assertions(1);
const rentalPromise = glm
.oneOf({ order })
.then(() => {
throw new Error("Rental should not be created");
})
.catch((err) => {
expect(err).toBe("Golem Network is disconnecting");
});
await glm.disconnect();
await rentalPromise;
verify(mockPayment.releaseAllocation(allocation)).once();
});
it("stops any manyOf() rentals that are in the process of being created", async () => {
const allocation = instance(mock(Allocation));
when(mockPayment.createAllocation(_)).thenResolve(allocation);
when(mockPayment.releaseAllocation(allocation)).thenResolve();

// simulate some operation taking a long time
when(mockPayment.createAllocation(_)).thenCall(async () => {
await sleep(100, true);
return allocation;
});

const glm = getGolemNetwork();
await glm.connect();

expect.assertions(1);
const rentalPromise = glm
.manyOf({ poolSize: 3, order })
.then(() => {
throw new Error("Pool should not be created");
})
.catch((err) => {
expect(err).toBe("Golem Network is disconnecting");
});
await glm.disconnect();
await rentalPromise;
verify(mockPayment.releaseAllocation(allocation)).once();
});
});
});
Loading

0 comments on commit 9abfafb

Please sign in to comment.