mirror of https://github.com/dapr/java-sdk.git
Fix bug in actor contains state API. (#268)
This commit is contained in:
parent
812020a366
commit
2bff7a1ca2
|
@ -37,7 +37,13 @@ class DaprHttpClient implements DaprClient {
|
|||
public Mono<byte[]> getActorState(String actorType, String actorId, String keyName) {
|
||||
String url = String.format(Constants.ACTOR_STATE_KEY_RELATIVE_URL_FORMAT, actorType, actorId, keyName);
|
||||
Mono<DaprHttp.Response> responseMono = this.client.invokeApi(DaprHttp.HttpMethods.GET.name(), url, null, "", null);
|
||||
return responseMono.map(r -> r.getBody());
|
||||
return responseMono.map(r -> {
|
||||
if ((r.getStatusCode() != 200) && (r.getStatusCode() != 204)) {
|
||||
throw new IllegalStateException(
|
||||
String.format("Error getting actor state: %s/%s/%s", actorType, actorId, keyName));
|
||||
}
|
||||
return r.getBody();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,7 +78,7 @@ class DaprStateAsyncProvider {
|
|||
|
||||
Mono<Boolean> contains(String actorType, ActorId actorId, String stateName) {
|
||||
Mono<byte[]> result = this.daprClient.getActorState(actorType, actorId.toString(), stateName);
|
||||
return result.map(s -> true).defaultIfEmpty(false);
|
||||
return result.map(s -> s.length > 0).defaultIfEmpty(false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -38,12 +38,20 @@ public class ActorStateIT extends BaseIT {
|
|||
60000);
|
||||
|
||||
String message = "This is a message to be saved and retrieved.";
|
||||
String name = "Jon Doe";
|
||||
ActorId actorId = new ActorId(Long.toString(System.currentTimeMillis()));
|
||||
String actorType = "StatefulActorTest";
|
||||
logger.debug("Building proxy ...");
|
||||
ActorProxyBuilder<ActorProxy> proxyBuilder = new ActorProxyBuilder(actorType, ActorProxy.class);
|
||||
ActorProxy proxy = proxyBuilder.build(actorId);
|
||||
|
||||
// Validate conditional read works.
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking readMessage where data is not present yet ... ");
|
||||
String result = proxy.invokeActorMethod("readMessage", String.class).block();
|
||||
assertNull(result);
|
||||
}, 5000);
|
||||
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking writeMessage ... ");
|
||||
proxy.invokeActorMethod("writeMessage", message).block();
|
||||
|
@ -69,6 +77,28 @@ public class ActorStateIT extends BaseIT {
|
|||
assertEquals(mydata.value, result.value);
|
||||
}, 5000);
|
||||
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking writeName ... ");
|
||||
proxy.invokeActorMethod("writeName", name).block();
|
||||
}, 5000);
|
||||
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking readName where data is probably still cached ... ");
|
||||
String result = proxy.invokeActorMethod("readName", String.class).block();
|
||||
assertEquals(name, result);
|
||||
}, 5000);
|
||||
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking writeName with empty content... ");
|
||||
proxy.invokeActorMethod("writeName", "").block();
|
||||
}, 5000);
|
||||
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking readName where empty content is probably still cached ... ");
|
||||
String result = proxy.invokeActorMethod("readName", String.class).block();
|
||||
assertEquals("", result);
|
||||
}, 5000);
|
||||
|
||||
logger.debug("Waiting, so actor can be deactivated ...");
|
||||
Thread.sleep(10000);
|
||||
|
||||
|
@ -97,5 +127,11 @@ public class ActorStateIT extends BaseIT {
|
|||
assertEquals(mydata.value, result.value);
|
||||
}, 5000);
|
||||
logger.debug("Finished testing actor string state.");
|
||||
|
||||
callWithRetry(() -> {
|
||||
logger.debug("Invoking readName where empty content is not cached ... ");
|
||||
String result = newProxy.invokeActorMethod("readName", String.class).block();
|
||||
assertEquals("", result);
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,10 @@ public interface StatefulActor {
|
|||
|
||||
String readMessage();
|
||||
|
||||
void writeName(String something);
|
||||
|
||||
String readName();
|
||||
|
||||
void writeData(MyData something);
|
||||
|
||||
MyData readData();
|
||||
|
|
|
@ -31,6 +31,20 @@ public class StatefulActorImpl extends AbstractActor implements StatefulActor {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeName(String something) {
|
||||
super.getActorStateManager().set("name", something).block();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readName() {
|
||||
if (super.getActorStateManager().contains("name").block()) {
|
||||
return super.getActorStateManager().get("name", String.class).block();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(MyData something) {
|
||||
super.getActorStateManager().set("mydata", something).block();
|
||||
|
|
Loading…
Reference in New Issue