Skip to content

Commit

Permalink
feat: rename lease to rental in GolemNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
SewerynKras committed Jun 19, 2024
1 parent f3de834 commit 8de6eff
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
36 changes: 18 additions & 18 deletions src/golem-network/golem-network.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Subject } from "rxjs";
import { ActivityModuleImpl } from "../activity";
import { LeaseModuleImpl, LeaseProcess, LeaseProcessPool } from "../lease-process";
import { RentalModuleImpl, ResourceRental, ResourceRentalPool } from "../resource-rental";
import { DraftOfferProposalPool, MarketModuleImpl, OfferProposal } from "../market";
import { NetworkModuleImpl } from "../network";
import { Allocation, PaymentModuleImpl } from "../payment";
Expand Down Expand Up @@ -29,7 +29,7 @@ const mockMarket = mock(MarketModuleImpl);
const mockPayment = mock(PaymentModuleImpl);
const mockActivity = mock(ActivityModuleImpl);
const mockNetwork = mock(NetworkModuleImpl);
const mockLease = mock(LeaseModuleImpl);
const mockRental = mock(RentalModuleImpl);
const mockYagna = mock(YagnaApi);
const mockPaymentApi = mock(PaymentApiAdapter);
const mockActivityApi = mock(ActivityApiAdapter);
Expand All @@ -42,7 +42,7 @@ afterEach(() => {
reset(mockMarket);
reset(mockPayment);
reset(mockNetwork);
reset(mockLease);
reset(mockRental);
reset(mockPaymentApi);
reset(mockActivityApi);
reset(mockMarketApi);
Expand All @@ -58,7 +58,7 @@ function getGolemNetwork() {
market: instance(mockMarket),
payment: instance(mockPayment),
network: instance(mockNetwork),
lease: instance(mockLease),
rental: instance(mockRental),
paymentApi: instance(mockPaymentApi),
activityApi: instance(mockActivityApi),
marketApi: instance(mockMarketApi),
Expand All @@ -70,11 +70,11 @@ function getGolemNetwork() {
describe("Golem Network", () => {
describe("oneOf()", () => {
it("should create a lease and clean it up when disconnected", async () => {
const mockLeaseProcess = mock(LeaseProcess);
const testProcess = instance(mockLeaseProcess);
const mockResourceRental = mock(ResourceRental);
const testProcess = instance(mockResourceRental);

when(mockLeaseProcess.finalize()).thenResolve();
when(mockLease.createLease(_, _, _)).thenReturn(testProcess);
when(mockResourceRental.finalize()).thenResolve();
when(mockRental.createResourceRental(_, _, _)).thenReturn(testProcess);

const draftProposal$ = new Subject<OfferProposal>();
when(mockMarket.collectDraftOfferProposals(_)).thenReturn(draftProposal$);
Expand All @@ -93,16 +93,16 @@ describe("Golem Network", () => {

await glm.disconnect();

verify(mockLeaseProcess.finalize()).once();
verify(mockResourceRental.finalize()).once();
verify(mockPayment.releaseAllocation(allocation)).once();
});
it("should not release the allocation if it was provided by the user", async () => {
const allocation = instance(mock(Allocation));

const mockLeaseProcess = mock(LeaseProcess);
const testProcess = instance(mockLeaseProcess);
when(mockLeaseProcess.finalize()).thenResolve();
when(mockLease.createLease(_, _, _)).thenReturn(testProcess);
const mockResourceRental = mock(ResourceRental);
const testProcess = instance(mockResourceRental);
when(mockResourceRental.finalize()).thenResolve();
when(mockRental.createResourceRental(_, _, _)).thenReturn(testProcess);

when(mockMarket.collectDraftOfferProposals(_)).thenReturn(new Subject<OfferProposal>());
jest.spyOn(DraftOfferProposalPool.prototype, "acquire").mockResolvedValue({} as OfferProposal);
Expand All @@ -121,7 +121,7 @@ describe("Golem Network", () => {

await glm.disconnect();

verify(mockLeaseProcess.finalize()).once();
verify(mockResourceRental.finalize()).once();
verify(mockPayment.createAllocation(_)).never();
verify(mockPayment.releaseAllocation(allocation)).never();
});
Expand All @@ -135,10 +135,10 @@ describe("Golem Network", () => {
const draftProposal$ = new Subject<OfferProposal>();
when(mockMarket.collectDraftOfferProposals(_)).thenReturn(draftProposal$);

const mockLeasePool = mock(LeaseProcessPool);
const mockLeasePool = mock(ResourceRentalPool);
when(mockLeasePool.drainAndClear()).thenResolve();
const leasePool = instance(mockLeasePool);
when(mockLease.createLeaseProcessPool(_, _, _)).thenReturn(leasePool);
when(mockRental.createResourceRentalPool(_, _, _)).thenReturn(leasePool);

const glm = getGolemNetwork();

Expand All @@ -160,10 +160,10 @@ describe("Golem Network", () => {
const allocation = instance(mock(Allocation));

when(mockMarket.collectDraftOfferProposals(_)).thenReturn(new Subject<OfferProposal>());
const mockLeasePool = mock(LeaseProcessPool);
const mockLeasePool = mock(ResourceRentalPool);
when(mockLeasePool.drainAndClear()).thenResolve();
const leasePool = instance(mockLeasePool);
when(mockLease.createLeaseProcessPool(_, _, _)).thenReturn(leasePool);
when(mockRental.createResourceRentalPool(_, _, _)).thenReturn(leasePool);

const glm = getGolemNetwork();
await glm.connect();
Expand Down
46 changes: 23 additions & 23 deletions src/golem-network/golem-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export interface GolemNetworkOptions {
payment: InstanceOrFactory<PaymentModule>;
activity: InstanceOrFactory<ActivityModule>;
network: InstanceOrFactory<NetworkModule>;
lease: InstanceOrFactory<RentalModule>;
rental: InstanceOrFactory<RentalModule>;
}
>;
}
Expand All @@ -120,7 +120,7 @@ type AllocationOptions = {
};

/**
* Represents the order specifications which will result in access to LeaseProcess.
* Represents the order specifications which will result in access to ResourceRental.
*/
export interface MarketOrderSpec {
demand: BuildDemandOptions;
Expand Down Expand Up @@ -195,7 +195,7 @@ export class GolemNetwork {

/**
* List af additional tasks that should be executed when the network is being shut down
* (for example finalizing lease processes created with `oneOf`)
* (for example finalizing resource rental created with `oneOf`)
*/
private readonly cleanupTasks: (() => Promise<void> | void)[] = [];

Expand Down Expand Up @@ -272,7 +272,7 @@ export class GolemNetwork {
this.activity = getFactory(ActivityModuleImpl, this.options.override?.activity)(this.services);
this.rental = getFactory(
RentalModuleImpl,
this.options.override?.lease,
this.options.override?.rental,
)({
activityModule: this.activity,
paymentModule: this.payment,
Expand Down Expand Up @@ -349,12 +349,12 @@ export class GolemNetwork {
*
* @example
* ```ts
* const lease = await glm.oneOf(demand);
* await lease
* const rental = await glm.oneOf(demand);
* await rental
* .getExeUnit()
* .then((exe) => exe.run("echo Hello, Golem! 👋"))
* .then((res) => console.log(res.stdout));
* await lease.finalize();
* await rental.finalize();
* ```
*
* @param order
Expand Down Expand Up @@ -385,7 +385,7 @@ export class GolemNetwork {
? await this.network.createNetworkNode(order.network, agreement.provider.id)
: undefined;

const lease = this.rental.createResourceRental(agreement, allocation, {
const rental = this.rental.createResourceRental(agreement, allocation, {
payment: order.payment,
activity: order.activity,
networkNode,
Expand All @@ -395,9 +395,9 @@ export class GolemNetwork {
proposalSubscription.unsubscribe();

this.cleanupTasks.push(async () => {
// First finalize the lease (which will wait for all payments to be processed)
// First finalize the rental (which will wait for all payments to be processed)
// and only then release the allocation
await lease.finalize().catch((err) => this.logger.error("Error while finalizing lease", err));
await rental.finalize().catch((err) => this.logger.error("Error while finalizing rental", err));
if (order.network && networkNode) {
await this.network
.removeNetworkNode(order.network, networkNode)
Expand All @@ -412,7 +412,7 @@ export class GolemNetwork {
.catch((err) => this.logger.error("Error while releasing allocation", err));
});

return lease;
return rental;
}

/**
Expand All @@ -421,26 +421,26 @@ export class GolemNetwork {
*
* @example
* ```ts
* // create a pool that can grow up to 3 leases at the same time
* // create a pool that can grow up to 3 rentals at the same time
* const pool = await glm.manyOf({
* concurrency: 3,
* demand
* });
* await Promise.allSettled([
* pool.withLease(async (lease) =>
* lease
* pool.withRental(async (rental) =>
* rental
* .getExeUnit()
* .then((exe) => exe.run("echo Hello, Golem from the first machine! 👋"))
* .then((res) => console.log(res.stdout)),
* ),
* pool.withLease(async (lease) =>
* lease
* pool.withRental(async (rental) =>
* rental
* .getExeUnit()
* .then((exe) => exe.run("echo Hello, Golem from the second machine! 👋"))
* .then((res) => console.log(res.stdout)),
* ),
* pool.withLease(async (lease) =>
* lease
* pool.withRental(async (rental) =>
* rental
* .getExeUnit()
* .then((exe) => exe.run("echo Hello, Golem from the third machine! 👋"))
* .then((res) => console.log(res.stdout)),
Expand All @@ -467,7 +467,7 @@ export class GolemNetwork {
});
const subscription = proposalPool.readFrom(draftProposal$);

const leaseProcessPool = this.rental.createResourceRentalPool(proposalPool, allocation, {
const resourceRentalPool = this.rental.createResourceRentalPool(proposalPool, allocation, {
replicas: concurrency,
network: order.network,
resourceRentalOptions: {
Expand All @@ -482,11 +482,11 @@ export class GolemNetwork {
subscription.unsubscribe();
});
this.cleanupTasks.push(async () => {
// First drain the pool (which will wait for all leases to be paid for)
// First drain the pool (which will wait for all rentals to be paid for
// and only then release the allocation
await leaseProcessPool
await resourceRentalPool
.drainAndClear()
.catch((err) => this.logger.error("Error while draining lease process pool", err));
.catch((err) => this.logger.error("Error while draining resource rental pool", err));
// Don't release the allocation if it was provided by the user
if (order.payment?.allocation) {
return;
Expand All @@ -496,7 +496,7 @@ export class GolemNetwork {
.catch((err) => this.logger.error("Error while releasing allocation", err));
});

return leaseProcessPool;
return resourceRentalPool;
}

isConnected() {
Expand Down
4 changes: 2 additions & 2 deletions src/market/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export enum MarketErrorCode {
ProposalResponseFailed = "ProposalResponseFailed",
ProposalRejectionFailed = "ProposalRejectionFailed",
DemandExpired = "DemandExpired",
ResourceRentalTerminationFailed = "LeaseProcessTerminationFailed",
ResourceRentalCreationFailed = "LeaseProcessCreationFailed",
ResourceRentalTerminationFailed = "ResourceRentalTerminationFailed",
ResourceRentalCreationFailed = "ResourceRentalCreationFailed",
AgreementApprovalFailed = "AgreementApprovalFailed",
NoProposalAvailable = "NoProposalAvailable",
InternalError = "InternalError",
Expand Down

0 comments on commit 8de6eff

Please sign in to comment.