-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add condition to terminate actors #36
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./commandAlreadyRun.js"; | ||
export * from "./commandNotRun.js"; | ||
export * from "./disputeNotFound.js"; | ||
export * from "./requestNotFound.js"; |
7 changes: 7 additions & 0 deletions
7
packages/automated-dispute/src/exceptions/eboRegistry/requestNotFound.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class RequestNotFound extends Error { | ||
constructor(requestId: string) { | ||
super(`Request ${requestId} was not found.`); | ||
|
||
this.name = "RequestNotFound"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/automated-dispute/src/services/eboRegistry/commands/finalizeRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { CommandAlreadyRun, CommandNotRun, RequestNotFound } from "../../../exceptions/index.js"; | ||
import { EboRegistry, EboRegistryCommand } from "../../../interfaces/index.js"; | ||
import { EboEvent, Request, RequestStatus } from "../../../types/index.js"; | ||
|
||
export class FinalizeRequest implements EboRegistryCommand { | ||
private wasRun: boolean = false; | ||
private previousStatus?: RequestStatus; | ||
|
||
private constructor( | ||
private readonly registry: EboRegistry, | ||
private readonly request: Request, | ||
) {} | ||
|
||
public static buildFromEvent( | ||
event: EboEvent<"RequestFinalized">, | ||
registry: EboRegistry, | ||
): FinalizeRequest { | ||
const requestId = event.metadata.requestId; | ||
const request = registry.getRequest(requestId); | ||
|
||
if (!request) throw new RequestNotFound(requestId); | ||
|
||
return new FinalizeRequest(registry, request); | ||
} | ||
|
||
run(): void { | ||
if (this.wasRun) throw new CommandAlreadyRun(FinalizeRequest.name); | ||
|
||
this.previousStatus = this.request.status; | ||
|
||
this.registry.updateRequestStatus(this.request.id, "Finalized"); | ||
|
||
this.wasRun = true; | ||
} | ||
|
||
undo(): void { | ||
if (!this.wasRun || !this.previousStatus) throw new CommandNotRun(FinalizeRequest.name); | ||
|
||
this.registry.updateRequestStatus(this.request.id, this.previousStatus); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/automated-dispute/src/services/eboRegistry/commands/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from "./addDispute.js"; | ||
export * from "./addRequest.js"; | ||
export * from "./addResponse.js"; | ||
export * from "./noop.js"; | ||
export * from "./finalizeRequest.js"; | ||
export * from "./updateDisputeStatus.js"; |
22 changes: 0 additions & 22 deletions
22
packages/automated-dispute/src/services/eboRegistry/commands/noop.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 48 additions & 23 deletions
71
packages/automated-dispute/tests/eboActor/onRequestFinalized.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,68 @@ | ||
import { ILogger } from "@ebo-agent/shared"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { FinalizeRequest } from "../../src/services/index.js"; | ||
import { EboEvent } from "../../src/types/index.js"; | ||
import mocks from "../mocks/index.js"; | ||
import { DEFAULT_MOCKED_REQUEST_CREATED_DATA } from "./fixtures.js"; | ||
|
||
const logger: ILogger = mocks.mockLogger(); | ||
|
||
describe("EboActor", () => { | ||
describe("onRequestFinalized", () => { | ||
const actorRequest = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
|
||
const event: EboEvent<"RequestFinalized"> = { | ||
name: "RequestFinalized", | ||
requestId: actorRequest.id, | ||
blockNumber: 1n, | ||
logIndex: 1, | ||
metadata: { | ||
blockNumber: 1n, | ||
caller: "0x01", | ||
describe("processEvents", () => { | ||
describe("when RequestFinalized is enqueued", () => { | ||
const actorRequest = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
|
||
const event: EboEvent<"RequestFinalized"> = { | ||
name: "RequestFinalized", | ||
requestId: actorRequest.id, | ||
responseId: "0x02", | ||
}, | ||
}; | ||
blockNumber: 1n, | ||
logIndex: 1, | ||
metadata: { | ||
blockNumber: 1n, | ||
caller: "0x01", | ||
requestId: actorRequest.id, | ||
responseId: "0x02", | ||
}, | ||
}; | ||
|
||
it("logs a message during request finalization", async () => { | ||
const { actor, registry } = mocks.buildEboActor(actorRequest, logger); | ||
|
||
vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); | ||
|
||
const mockInfo = vi.spyOn(logger, "info"); | ||
|
||
actor.enqueue(event); | ||
|
||
await actor.processEvents(); | ||
|
||
expect(mockInfo).toHaveBeenCalledWith( | ||
expect.stringMatching(`Request ${actorRequest.id} has been finalized.`), | ||
); | ||
}); | ||
|
||
it("uses the FinalizeRequest registry command", async () => { | ||
const { actor, registry } = mocks.buildEboActor(actorRequest, logger); | ||
|
||
it("logs a message during request finalization", async () => { | ||
const { actor, registry } = mocks.buildEboActor(actorRequest, logger); | ||
vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); | ||
|
||
vi.spyOn(registry, "getRequest").mockReturnValue(actorRequest); | ||
const mockFinalizeRequest = { | ||
run: vi.fn(), | ||
undo: vi.fn(), | ||
} as unknown as FinalizeRequest; | ||
|
||
const mockInfo = vi.spyOn(logger, "info"); | ||
const mockBuildFromEvent = vi | ||
.spyOn(FinalizeRequest, "buildFromEvent") | ||
.mockReturnValue(mockFinalizeRequest); | ||
|
||
actor.enqueue(event); | ||
actor.enqueue(event); | ||
|
||
await actor.processEvents(); | ||
await actor.processEvents(); | ||
|
||
expect(mockInfo).toHaveBeenCalledWith( | ||
expect.stringMatching(`Request ${actorRequest.id} has been finalized.`), | ||
); | ||
expect(mockBuildFromEvent).toHaveBeenCalledWith(event, registry); | ||
expect(mockFinalizeRequest.run).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we move this entire method to an isolated CommandsFactory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a
TODO
comment already, in the docs of this method, for doing that hehe