Skip to content

Commit 9fde09c

Browse files
ruokun-niuRuokun Niu
andauthored
fix(actor): Configured the delete operation in the state provider (#486)
* fix(actor): Configured the delete operation in the state provider Signed-off-by: Ruokun Niu <[email protected]> * test(actors): added a test for deleting the actor state Signed-off-by: Ruokun Niu <[email protected]> * test(actor): fixed the failed test for deleting an actor state Signed-off-by: Ruokun Niu <[email protected]> * test(actor): ran pretty-fix Signed-off-by: Ruokun Niu <[email protected]> --------- Signed-off-by: Ruokun Niu <[email protected]> Co-authored-by: Ruokun Niu <[email protected]>
1 parent a3ca988 commit 9fde09c

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

src/actors/runtime/StateProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class StateProvider {
7676
operation = "upsert";
7777
break;
7878
case StateChangeKind.REMOVE:
79-
operation = "remove";
79+
operation = "delete";
8080
break;
8181
case StateChangeKind.UPDATE:
8282
// dapr doesn't know add, we use upsert
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2022 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { AbstractActor } from "../../src";
15+
import DemoActorDeleteStateInterface from "./DemoActorDeleteStateInterface";
16+
17+
export default class DemoActorDeleteStateImpl extends AbstractActor implements DemoActorDeleteStateInterface {
18+
async init(): Promise<string> {
19+
await this.getStateManager().getOrAddState("data", "Hello World!");
20+
21+
return "state initialized";
22+
}
23+
24+
async tryGetState(): Promise<boolean | null> {
25+
const [hasValue, _] = await this.getStateManager().tryGetState("data");
26+
return hasValue;
27+
}
28+
29+
async deleteState(key: string): Promise<void> {
30+
await this.getStateManager().removeState(key);
31+
await this.getStateManager().saveState();
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2022 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
export default interface DemoActorCounterInterface {
15+
init(): Promise<string>;
16+
tryGetState(): Promise<boolean | null>;
17+
deleteState(key: string): Promise<void>;
18+
}

test/e2e/http/actors.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import DemoActorTimerImpl from "../../actor/DemoActorTimerImpl";
2929
import DemoActorTimerInterface from "../../actor/DemoActorTimerInterface";
3030
import DemoActorTimerTtlImpl from "../../actor/DemoActorTimerTtlImpl";
3131
import DemoActorReminderTtlImpl from "../../actor/DemoActorReminderTtlImpl";
32+
import DemoActorDeleteStateImpl from "../../actor/DemoActorDeleteStateImpl";
33+
import DemoActorDeleteStateInterface from "../../actor/DemoActorDeleteStateInterface";
3234

3335
const serverHost = "127.0.0.1";
3436
const serverPort = "50001";
@@ -84,6 +86,7 @@ describe("http/actors", () => {
8486
await server.actor.registerActor(DemoActorActivateImpl);
8587
await server.actor.registerActor(DemoActorTimerTtlImpl);
8688
await server.actor.registerActor(DemoActorReminderTtlImpl);
89+
await server.actor.registerActor(DemoActorDeleteStateImpl);
8790

8891
// Start server
8992
await server.start(); // Start the general server, this can take a while
@@ -106,7 +109,7 @@ describe("http/actors", () => {
106109

107110
const config = JSON.parse(await res.text());
108111

109-
expect(config.entities.length).toBe(8);
112+
expect(config.entities.length).toBe(9);
110113
expect(config.actorIdleTimeout).toBe("1h");
111114
expect(config.actorScanInterval).toBe("30s");
112115
expect(config.drainOngoingCallTimeout).toBe("1m");
@@ -152,11 +155,27 @@ describe("http/actors", () => {
152155
});
153156
});
154157

158+
describe("deleteActorState", () => {
159+
it("should be able to delete actor state", async () => {
160+
const builder = new ActorProxyBuilder<DemoActorDeleteStateInterface>(DemoActorDeleteStateImpl, client);
161+
const actor = builder.build(ActorId.createRandomId());
162+
await actor.init();
163+
164+
const res = await actor.tryGetState();
165+
expect(res).toEqual(true);
166+
167+
await actor.deleteState("data");
168+
169+
const deletedRes = await actor.tryGetState();
170+
console.log(deletedRes);
171+
expect(deletedRes).toEqual(false);
172+
});
173+
});
155174
describe("invoke", () => {
156175
it("should register actors correctly", async () => {
157176
const actors = await server.actor.getRegisteredActors();
158177

159-
expect(actors.length).toEqual(8);
178+
expect(actors.length).toEqual(9);
160179

161180
expect(actors).toContain(DemoActorCounterImpl.name);
162181
expect(actors).toContain(DemoActorSayImpl.name);

0 commit comments

Comments
 (0)