use of io.reactivex.rxjava3.functions.Function4 in project comet-java-sdk by comet-ml.
the class ArtifactSupportTest method testLogAndGetArtifact.
@Test
public void testLogAndGetArtifact() {
try (OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment()) {
ArtifactImpl artifact = createArtifact();
// add remote assets
//
URI firstAssetLink = new URI("s3://bucket/folder/firstAssetFile.extension");
String firstAssetFileName = "firstAssetFileName";
artifact.addRemoteAsset(firstAssetLink, firstAssetFileName);
String secondAssetExpectedFileName = "secondAssetFile.extension";
URI secondAssetLink = new URI("s3://bucket/folder/" + secondAssetExpectedFileName);
artifact.addRemoteAsset(secondAssetLink, secondAssetExpectedFileName);
// add local assets
//
artifact.addAsset(Objects.requireNonNull(TestUtils.getFile(IMAGE_FILE_NAME)), IMAGE_FILE_NAME, false, SOME_METADATA);
artifact.addAsset(Objects.requireNonNull(TestUtils.getFile(CODE_FILE_NAME)), CODE_FILE_NAME, false);
byte[] someData = "some data".getBytes(StandardCharsets.UTF_8);
String someDataName = "someDataName";
artifact.addAsset(someData, someDataName);
// add assets folder
//
artifact.addAssetFolder(assetsFolder.toFile(), true, true);
// the logged artifact validator
Function4<LoggedArtifact, ArtifactImpl, String, List<String>, Void> loggedArtifactValidator = (actual, original, experimentKey, expectedAliases) -> {
assertNotNull(actual, "logged artifact expected");
assertEquals(original.getType(), actual.getArtifactType(), "wrong artifact type");
assertEquals(new HashSet<>(expectedAliases), actual.getAliases(), "wrong aliases");
assertEquals(SOME_METADATA, actual.getMetadata(), "wrong metadata");
assertEquals(new HashSet<>(original.getVersionTags()), actual.getVersionTags(), "wrong version tags");
assertEquals(WORKSPACE_NAME, actual.getWorkspace(), "wrong workspace");
assertEquals(experimentKey, actual.getSourceExperimentKey(), "wrong experiment key");
assertEquals(original.getName(), actual.getName(), "wrong artifact name");
return null;
};
// check artifacts-in-progress counter before
assertEquals(0, experiment.getArtifactsInProgress().get(), "artifacts-in-progress counter must be zero at start");
// log artifact and check results
//
CompletableFuture<LoggedArtifact> futureArtifact = experiment.logArtifact(artifact);
// check artifacts-in-progress counter while in progress
assertEquals(1, experiment.getArtifactsInProgress().get(), "artifacts-in-progress counter has wrong value while still in progress");
LoggedArtifact loggedArtifact = futureArtifact.get(60, SECONDS);
// check artifacts-in-progress counter after
Awaitility.await("artifacts-in-progress counter must be decreased").pollInterval(10, TimeUnit.MILLISECONDS).atMost(1, TimeUnit.SECONDS).until(() -> experiment.getArtifactsInProgress().get() == 0);
assertEquals(0, experiment.getArtifactsInProgress().get(), "artifacts-in-progress counter must be zero after log operation completed");
List<String> expectedAliases = new ArrayList<>(artifact.getAliases());
loggedArtifactValidator.apply(loggedArtifact, artifact, experiment.getExperimentKey(), expectedAliases);
// get artifact details from server and check its correctness
//
LoggedArtifact loggedArtifactFromServer = experiment.getArtifact(loggedArtifact.getName(), loggedArtifact.getWorkspace(), loggedArtifact.getVersion());
// added by the backend automatically
expectedAliases.add(ALIAS_LATEST);
loggedArtifactValidator.apply(loggedArtifactFromServer, artifact, experiment.getExperimentKey(), expectedAliases);
// check that correct assets was logged
//
Collection<LoggedArtifactAsset> loggedAssets = loggedArtifactFromServer.getAssets();
Collection<ArtifactAsset> assets = artifact.getAssets();
assertEquals(assets.size(), loggedAssets.size(), "wrong size");
loggedAssets.forEach(loggedArtifactAsset -> validateArtifactAsset(new ArtifactAssetImpl((LoggedArtifactAssetImpl) loggedArtifactAsset), assets));
} catch (Throwable t) {
fail(t);
}
}
Aggregations