From 383be89959d0cfb3dacdfc7734b841f56096635a Mon Sep 17 00:00:00 2001 From: "elena.parovyshnaya" Date: Mon, 4 May 2020 20:50:48 +0300 Subject: [PATCH 1/2] Bug 560445 - [YARS] support incremental monitoring - add tests for Progress contract Signed-off-by: elena.parovyshnaya --- .../yars/internal/api/SingleSwoopExport.java | 14 +++- .../loc/yars/internal/api/export/All.java | 4 +- .../internal/api/export/CountingProgress.java | 74 +++++++++++++++++++ .../loc/yars/internal/api/export/Csv.java | 6 +- .../yars/internal/api/export/Enlistment.java | 2 +- .../{ExportedEntry.java => ExportEntry.java} | 9 ++- .../yars/internal/api/export/ExportTest.java | 22 +++--- .../loc/yars/internal/api/export/Fetch.java | 6 +- .../loc/yars/internal/api/export/Json.java | 6 +- .../internal/api/export/ProgressTest.java | 63 ++++++++++++++++ .../internal/api/model/InMemoryStorage.java | 6 +- 11 files changed, 180 insertions(+), 32 deletions(-) create mode 100644 tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/CountingProgress.java rename tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/{ExportedEntry.java => ExportEntry.java} (81%) create mode 100644 tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java diff --git a/bundles/org.eclipse.passage.loc.yars.api/src/org/eclipse/passage/loc/yars/internal/api/SingleSwoopExport.java b/bundles/org.eclipse.passage.loc.yars.api/src/org/eclipse/passage/loc/yars/internal/api/SingleSwoopExport.java index 40ca3d558..5430096e8 100644 --- a/bundles/org.eclipse.passage.loc.yars.api/src/org/eclipse/passage/loc/yars/internal/api/SingleSwoopExport.java +++ b/bundles/org.eclipse.passage.loc.yars.api/src/org/eclipse/passage/loc/yars/internal/api/SingleSwoopExport.java @@ -65,17 +65,23 @@ public SingleSwoopExport(FetchedData query) { @Override public void write(DosHandleMedia media, Progress progress) { media.start(); + writeData(media, progress); + media.finish(); + } + + private void writeData(DosHandleMedia media, Progress progress) { List fetch = query.get(); progress.estimate(fetch.size()); - - fetch.forEach(data -> { + for (T data : fetch) { + if (progress.cancelDemanded()) { + break; + } progress.reportNodeSrart(data); media.startNode(data); data.write(media, progress); media.finishNode(data); progress.reportNodeFinish(data); - }); - media.finish(); + } } } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/All.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/All.java index e259fb3f7..f7523bd9f 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/All.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/All.java @@ -18,7 +18,7 @@ import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; @SuppressWarnings("restriction") -public class All implements Query { +final class All implements Query { @Override public String id() { @@ -31,7 +31,7 @@ public String description() { } @Override - public FetchedData fetch(InMemoryStorage base, FetchParams params) { + public FetchedData fetch(InMemoryStorage base, FetchParams params) { return new Fetch(base); } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/CountingProgress.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/CountingProgress.java new file mode 100644 index 000000000..45bf5d2a6 --- /dev/null +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/CountingProgress.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.yars.internal.api.export; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.passage.loc.yars.internal.api.Progress; +import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; + +@SuppressWarnings("restriction") +final class CountingProgress implements Progress { + + private final InMemoryStorage storage; + private final int limit; + private int estimated = 0; + private int started = 0; + private int finished = 0; + private List infos = new ArrayList<>(); + + CountingProgress(InMemoryStorage storage, int limit) { + this.storage = storage; + this.limit = limit; + } + + @Override + public void estimate(int amount) { + estimated = amount; + } + + @Override + public void reportNodeSrart(ExportEntry entry) { + started++; + } + + @Override + public void reportNodeFinish(ExportEntry entry) { + finished++; + } + + @Override + public void report(String info) { + this.infos.add(info); + } + + @Override + public boolean cancelDemanded() { + return finished >= limit; + } + + void assertContractIsOk() { + assertTrue(started == finished); + assertTrue(started <= estimated); + } + + void assertStateIsOk() { + assertEquals(storage.size(), estimated); + assertTrue(started == limit); + } + +} diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Csv.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Csv.java index 821ce16f4..5cfe70325 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Csv.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Csv.java @@ -18,7 +18,7 @@ import org.eclipse.passage.loc.yars.internal.api.ListMedia; @SuppressWarnings("restriction") -class Csv implements ListMedia { +final class Csv implements ListMedia { private final StringBuilder builder; private final List header; @@ -35,7 +35,7 @@ public final void start() { } @Override - public final void startNode(ExportedEntry node) { + public final void startNode(ExportEntry node) { builder.append("\n"); //$NON-NLS-1$ } @@ -49,7 +49,7 @@ public final void finish() { } @Override - public final void finishNode(ExportedEntry node) { + public final void finishNode(ExportEntry node) { } } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Enlistment.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Enlistment.java index 609bb6a45..7f4341abc 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Enlistment.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Enlistment.java @@ -17,7 +17,7 @@ import org.eclipse.passage.loc.yars.internal.api.ListMedia; @SuppressWarnings("restriction") -public class Enlistment implements ListMedia { +final class Enlistment implements ListMedia { private final List content; diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportedEntry.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportEntry.java similarity index 81% rename from tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportedEntry.java rename to tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportEntry.java index d7b8ffe7f..baa3d0b92 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportedEntry.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportEntry.java @@ -19,17 +19,18 @@ import org.eclipse.passage.loc.yars.internal.api.Progress; @SuppressWarnings("restriction") -public class ExportedEntry implements ExportData> { +final class ExportEntry implements ExportData> { private String name; - public ExportedEntry(String name) { + public ExportEntry(String name) { this.name = name; } @Override - public void write(DosHandleMedia media, Progress p) { + public void write(DosHandleMedia media, Progress progress) { media.inner(name, "name"); //$NON-NLS-1$ + progress.report(name); } @Override // generated @@ -38,7 +39,7 @@ public boolean equals(Object o) { return true; if (o == null || getClass() != o.getClass()) return false; - ExportedEntry exportedEntry = (ExportedEntry) o; + ExportEntry exportedEntry = (ExportEntry) o; return Objects.equals(name, exportedEntry.name); } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportTest.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportTest.java index ff19a4c16..9089261e0 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportTest.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ExportTest.java @@ -52,7 +52,7 @@ *
  • We define a {@linkplain Query} - {@linkplain All} - which fetches all * entries from our storage. It also emulates a business logic that is * implemented as a conversion stored entries to entities of another type - * ({@linkplain ExportedEntry})
  • + * ({@linkplain ExportEntry}) *
  • When we as our query for {@linkplain Query#data()}, it actually * does not interacts with the {@code storage}, but only instantiate a dedicated * {@linkplain FetchedData} instance.
  • @@ -63,7 +63,7 @@ * runtime list. All of them are unaware of storing and fetching details. */ @SuppressWarnings("restriction") -public class ExportTest { +public final class ExportTest { @Test public void testCsv() { @@ -75,12 +75,12 @@ public void testCsv() { @Test public void testEnlistment() { - List output = new ArrayList<>(); - queryResult(new Enlistment(output)); + List output = new ArrayList<>(); + queryResult(new Enlistment(output)); assertEquals(Arrays.asList( // - new ExportedEntry("Gammy"), //$NON-NLS-1$ - new ExportedEntry("Quami"), //$NON-NLS-1$ - new ExportedEntry("Tsunami")), //$NON-NLS-1$ + new ExportEntry("Gammy"), //$NON-NLS-1$ + new ExportEntry("Quami"), //$NON-NLS-1$ + new ExportEntry("Tsunami")), //$NON-NLS-1$ output); } @@ -102,8 +102,8 @@ public void testJson() { output.toString()); } - private void queryResult(ListMedia media) { - new SingleSwoopExport(new All().fetch(// + private void queryResult(ListMedia media) { + new SingleSwoopExport(new All().fetch(// new InMemoryStorage( // new StoredEntry("Gammy", "US"), //$NON-NLS-1$ //$NON-NLS-2$ new StoredEntry("Quami", "France"), //$NON-NLS-1$ //$NON-NLS-2$ @@ -111,10 +111,10 @@ private void queryResult(ListMedia media) { ), // new FetchParams.Empty()))// .write(// - new DosHandleMedia(// + new DosHandleMedia(// media, // new DefaultDosHandler()), // - new Progress.Inane()); + new Progress.Inane()); } } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Fetch.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Fetch.java index 8c2b46135..bc30e421c 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Fetch.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Fetch.java @@ -19,7 +19,7 @@ import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; @SuppressWarnings("restriction") -class Fetch implements FetchedData { +final class Fetch implements FetchedData { private final InMemoryStorage source; @@ -28,9 +28,9 @@ class Fetch implements FetchedData { } @Override - public List get() { + public List get() { return source.entries().stream()// - .map(dummy -> new ExportedEntry(dummy.name()))// + .map(dummy -> new ExportEntry(dummy.name()))// .collect(Collectors.toList()); } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Json.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Json.java index 0f9f15af8..451faf209 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Json.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/Json.java @@ -15,7 +15,7 @@ import org.eclipse.passage.loc.yars.internal.api.ListMedia; @SuppressWarnings("restriction") -class Json implements ListMedia { +final class Json implements ListMedia { private final StringBuilder builder; @@ -35,12 +35,12 @@ public final void finish() { } @Override - public final void startNode(ExportedEntry node) { + public final void startNode(ExportEntry node) { builder.append("\t\"node\" : {\n"); //$NON-NLS-1$ } @Override - public final void finishNode(ExportedEntry node) { + public final void finishNode(ExportEntry node) { builder.append("\t}\n"); //$NON-NLS-1$ } diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java new file mode 100644 index 000000000..d8e2afce2 --- /dev/null +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java @@ -0,0 +1,63 @@ +package org.eclipse.passage.loc.yars.internal.api.export; + +import java.util.ArrayList; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.eclipse.passage.loc.yars.internal.api.DefaultDosHandler; +import org.eclipse.passage.loc.yars.internal.api.DosHandleMedia; +import org.eclipse.passage.loc.yars.internal.api.FetchParams; +import org.eclipse.passage.loc.yars.internal.api.ListMedia; +import org.eclipse.passage.loc.yars.internal.api.Progress; +import org.eclipse.passage.loc.yars.internal.api.SingleSwoopExport; +import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage; +import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry; +import org.junit.Test; + +@SuppressWarnings("restriction") +public final class ProgressTest { + + @Test + public void ticks() { + exportWithProgress(InMemoryStorage::size); + } + + @Test + public void cancells() { + exportWithProgress(storage -> storage.size() / 3); + } + + private void exportWithProgress(Function limit) { + InMemoryStorage storage = storage(); + CountingProgress progress = new CountingProgress(storage, limit.apply(storage)); + queryResult(new Enlistment(new ArrayList<>()), progress); + progress.assertContractIsOk(); + progress.assertStateIsOk(); + } + + private InMemoryStorage storage() { + return new InMemoryStorage( // + IntStream.range(1, 16) // + .mapToObj(i -> new StoredEntry(Integer.toString(i), Integer.toBinaryString(i))) // + .collect(Collectors.toList()) // + ); + } + + private void queryResult(ListMedia media, Progress progress) { + InMemoryStorage storage = new InMemoryStorage( // + IntStream.range(1, 16) // + .mapToObj(i -> new StoredEntry(Integer.toString(i), Integer.toBinaryString(i))) // + .collect(Collectors.toList()) // + ); + new SingleSwoopExport(new All().fetch(// + storage, // + new FetchParams.Empty()))// + .write(// + new DosHandleMedia(// + media, // + new DefaultDosHandler()), // + progress); + } + +} diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/model/InMemoryStorage.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/model/InMemoryStorage.java index 4d92558a9..cf9d709d3 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/model/InMemoryStorage.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/model/InMemoryStorage.java @@ -22,7 +22,7 @@ public class InMemoryStorage implements Storage { private final List dummies; - InMemoryStorage(List dummies) { + public InMemoryStorage(List dummies) { this.dummies = dummies; } @@ -34,4 +34,8 @@ public List entries() { return dummies; } + public int size() { + return dummies.size(); + } + } From 13449db97db72271ac9b8a283eb81e597dbc4b0a Mon Sep 17 00:00:00 2001 From: "elena.parovyshnaya" Date: Tue, 5 May 2020 20:21:51 +0300 Subject: [PATCH 2/2] Bug 560445 - [YARS] support incremental monitoring - add license header to ProgressTest Signed-off-by: elena.parovyshnaya --- .../loc/yars/internal/api/export/ProgressTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java index d8e2afce2..41301bf14 100644 --- a/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java +++ b/tests/org.eclipse.passage.loc.yars.api.tests/src/org/eclipse/passage/loc/yars/internal/api/export/ProgressTest.java @@ -1,3 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ package org.eclipse.passage.loc.yars.internal.api.export; import java.util.ArrayList;