This is an automated email from the ASF dual-hosted git repository. jinglun pushed a commit to branch HADOOP-19236-original in repository https://gitbox.apache.org/repos/asf/hadoop.git
commit 72ce7cee0923656c15b228edac359018b16024bc Author: lijinglun <lijing...@bytedance.com> AuthorDate: Thu Aug 15 19:20:42 2024 +0800 Integration of TOS: Add lazy reload interface to common. test --- .../hadoop/fs/tosfs/util/TestLazyReload.java | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/hadoop-cloud-storage-project/hadoop-tos/src/test/java/org/apache/hadoop/fs/tosfs/util/TestLazyReload.java b/hadoop-cloud-storage-project/hadoop-tos/src/test/java/org/apache/hadoop/fs/tosfs/util/TestLazyReload.java new file mode 100644 index 00000000000..de4ebf9ffaf --- /dev/null +++ b/hadoop-cloud-storage-project/hadoop-tos/src/test/java/org/apache/hadoop/fs/tosfs/util/TestLazyReload.java @@ -0,0 +1,93 @@ +/* + * 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.hadoop.fs.tosfs.util; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Iterator; + +public class TestLazyReload { + @Test + public void testLoadWithFilterCondition() { + LazyReload<Integer> integers = new LazyReload<>(() -> { + Iterator<Integer> source = Arrays.asList(1, 3, 5, 2, 4, 6).iterator(); + return buf -> { + if (!source.hasNext()) { + return true; + } + + int pollCnt = 2; + while (source.hasNext() && pollCnt-- > 0) { + Integer item = source.next(); + if (item % 2 == 0) { + buf.add(item); + } + } + + return !source.hasNext(); + }; + }); + + Iterator<Integer> iterator = integers.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertEquals(2, (int) iterator.next()); + Assert.assertEquals(4, (int) iterator.next()); + Assert.assertEquals(6, (int) iterator.next()); + Assert.assertFalse(iterator.hasNext()); + } + + @Test + public void testLoadResultIsIdempotent() { + LazyReload<Integer> integers = new LazyReload<>(() -> { + Iterator<Integer> source = Arrays.asList(1, 3, 5, 2, 4, 6).iterator(); + return buf -> { + if (!source.hasNext()) { + return true; + } + + int pollCnt = 2; + while (source.hasNext() && pollCnt-- > 0) { + Integer item = source.next(); + buf.add(item); + } + + return !source.hasNext(); + }; + }); + Iterator<Integer> iterator1 = integers.iterator(); + Iterator<Integer> iterator2 = integers.iterator(); + + Assert.assertEquals(1, (int) iterator1.next()); + Assert.assertEquals(1, (int) iterator2.next()); + Assert.assertEquals(3, (int) iterator1.next()); + Assert.assertEquals(3, (int) iterator2.next()); + Assert.assertEquals(5, (int) iterator1.next()); + Assert.assertEquals(5, (int) iterator2.next()); + + Assert.assertEquals(2, (int) iterator1.next()); + Assert.assertEquals(4, (int) iterator1.next()); + Assert.assertEquals(6, (int) iterator1.next()); + Assert.assertEquals(2, (int) iterator2.next()); + Assert.assertEquals(4, (int) iterator2.next()); + Assert.assertEquals(6, (int) iterator2.next()); + } +} + --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org For additional commands, e-mail: common-commits-h...@hadoop.apache.org