-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[Java SDK] Add ValueState.read(T defaultValue) overload #37824
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.beam.sdk.state; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertSame; | ||
|
|
||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** Tests for {@link ValueState}. */ | ||
| @RunWith(JUnit4.class) | ||
| public class ValueStateTest { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests for the new functionality are a good start. To make them more comprehensive, consider adding test cases for edge scenarios. Specifically, testing the behavior after For example: @Test
public void testReadReturnsDefaultValueAfterClear() {
TestValueState<Integer> state = new TestValueState<>();
state.write(10);
state.clear();
assertEquals(Integer.valueOf(5), state.read(5));
}
@Test
public void testReadReturnsDefaultValueWhenNullIsWritten() {
TestValueState<Integer> state = new TestValueState<>();
state.write(null);
assertEquals(Integer.valueOf(5), state.read(5));
} |
||
|
|
||
| @Test | ||
| public void testReadReturnsDefaultValueWhenStateIsEmpty() { | ||
| ValueState<Integer> state = new TestValueState<>(); | ||
| assertEquals(Integer.valueOf(5), state.read(5)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadReturnsStoredValueWhenStateIsPresent() { | ||
| TestValueState<Integer> state = new TestValueState<>(); | ||
| state.write(10); | ||
| assertEquals(Integer.valueOf(10), state.read(5)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadLaterReturnsSameState() { | ||
| ValueState<Integer> state = new TestValueState<>(); | ||
| assertSame(state, state.readLater()); | ||
| } | ||
|
|
||
| private static class TestValueState<T> implements ValueState<T> { | ||
|
|
||
| private @Nullable T value; | ||
|
|
||
| @Override | ||
| public void write(T input) { | ||
| value = input; | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable T read() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public ValueState<T> readLater() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| value = null; | ||
| } | ||
| } | ||
| } | ||
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.
The Javadoc states that
defaultValueis returned 'if the value has never been written'. However, the implementation returnsdefaultValuewheneverread()returnsnull. This can occur not only when the state is uninitialized, but also whennullhas been explicitly written or the state has been cleared. To avoid confusion, the Javadoc should be updated to accurately reflect this behavior.