tzulitai commented on a change in pull request #177: URL: https://github.com/apache/flink-statefun/pull/177#discussion_r529314801
########## File path: statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/reqreply/PersistedRemoteFunctionValuesTest.java ########## @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.statefun.flink.core.reqreply; + +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import com.google.protobuf.ByteString; +import java.util.Arrays; +import java.util.Collections; +import org.apache.flink.statefun.flink.core.polyglot.generated.FromFunction.PersistedValueMutation; +import org.apache.flink.statefun.flink.core.polyglot.generated.FromFunction.PersistedValueSpec; +import org.apache.flink.statefun.flink.core.polyglot.generated.ToFunction.InvocationBatchRequest; +import org.apache.flink.statefun.flink.core.polyglot.generated.ToFunction.PersistedValue; +import org.junit.Test; + +public class PersistedRemoteFunctionValuesTest { + + @Test + public void exampleUsage() { + final PersistedRemoteFunctionValues values = + new PersistedRemoteFunctionValues(Collections.emptyList()); + + // --- register persisted states + values.registerStates( + Arrays.asList( + protocolPersistedValueSpec("state-1"), protocolPersistedValueSpec("state-2"))); + + // --- update state values + values.updateStateValues( + Arrays.asList( + protocolPersistedValueModifyMutation("state-1", ByteString.copyFromUtf8("data-1")), + protocolPersistedValueModifyMutation("state-2", ByteString.copyFromUtf8("data-2")))); + + final InvocationBatchRequest.Builder builder = InvocationBatchRequest.newBuilder(); + values.attachStateValues(builder); + + // --- registered state names and their values should be attached + assertThat(builder.getStateList().size(), is(2)); + assertThat( + builder.getStateList(), + hasItems( + protocolPersistedValue("state-1", ByteString.copyFromUtf8("data-1")), + protocolPersistedValue("state-2", ByteString.copyFromUtf8("data-2")))); + } + + @Test + public void zeroRegisteredStates() { + final PersistedRemoteFunctionValues values = + new PersistedRemoteFunctionValues(Collections.emptyList()); + + final InvocationBatchRequest.Builder builder = InvocationBatchRequest.newBuilder(); + values.attachStateValues(builder); + + assertThat(builder.getStateList().size(), is(0)); + } + + @Test(expected = IllegalStateException.class) Review comment: 👍 Food for thought: I do think it is the SDK's responsibility to catch this though: if a function is accessing state `foobar` but this state wasn't declared, then an exception should be thrown already on the function's side. Since from the `RequestReplyFunction` in StateFun's point of view there is no way to enforce this, I guess it does make sense to make this exception more descriptive. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
