Skip to content

Commit

Permalink
Switch sampler to use more of Spring to make the code simplier
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Suszynski committed May 24, 2019
1 parent 8047231 commit e13a0be
Show file tree
Hide file tree
Showing 35 changed files with 695 additions and 540 deletions.
7 changes: 6 additions & 1 deletion tools/plugs-packager-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<!-- Tests -->
<dependency>
<groupId>pl.wavesoftware.plugs.tools</groupId>
<artifactId>plugs-packager-testing</artifactId>
<artifactId>plugs-packager-samples</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -67,6 +67,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigest;
import pl.wavesoftware.plugs.tools.packager.api.model.Project;
import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext;
import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext;
import pl.wavesoftware.plugs.tools.packager.sample.project.Projects;
import pl.wavesoftware.plugs.tools.packager.testing.SamplerExtension;
import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject;

import java.io.IOException;

Expand All @@ -32,14 +35,18 @@
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a>
* @since 0.1.0
*/
@ExtendWith(SamplerExtension.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = PackagerSamplerContext.class)
class ProjectDigestImplTest {

@Autowired
private SamplerContext context;

@Test
void digest(SamplerContext context) throws IOException {
void digest() throws IOException {
// given
ProjectDigest digester = new ProjectDigestImpl();
Project project = context.get(Projects.SIMPLE);
Project project = context.get(SimpleProject.class);

// when
CharSequence digest = digester.digest(project);
Expand Down
5 changes: 5 additions & 0 deletions tools/plugs-packager-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019 Wave Software
*
* Licensed 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 pl.wavesoftware.plugs.tools.packager.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

abstract class AbstractSamplerContext implements SamplerContext {
private static final Logger LOGGER =
LoggerFactory.getLogger(AbstractSamplerContext.class);
private final Map<Class<? extends Sampler<?>>, Object> samples =
new HashMap<>();

@Override
public <T> T get(Class<? extends Sampler<T>> spec) {
@SuppressWarnings("unchecked")
T val = (T) samples.computeIfAbsent(spec, ignored -> createNew(spec));
return val;
}

@Override
public <T> T createNew(Class<? extends Sampler<T>> spec) {
T sample = getSampler(spec).create();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"Created sample {} for spec {}",
Integer.toHexString(sample.hashCode()), spec
);
}
return sample;
}

@Override
public void destroy() {
samples.clear();
}

protected abstract <T> Sampler<T> getSampler(Class<? extends Sampler<T>> spec);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@

package pl.wavesoftware.plugs.tools.packager.sample;

import java.util.function.Supplier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

public interface Sample<T> extends Supplier<T>, AutoCloseable {
@Override
void close();
@ComponentScan(includeFilters = @ComponentScan.Filter(Sampler.Component.class))
@Import(SamplerContext.Configuration.class)
public class PackagerSamplerContext {

interface Easy<T> extends Sample<T> {
@Override
default void close() {
// nothing here
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,18 @@

package pl.wavesoftware.plugs.tools.packager.sample;

import io.vavr.Lazy;

import java.util.Random;
import java.util.function.Supplier;

final class SamplerContextProxy implements SamplerContext {

private final Lazy<SamplerContext> context;
final class RandomSeedSupplier implements Supplier<CharSequence> {

SamplerContextProxy(Supplier<SamplerContext> supplier) {
this.context = Lazy.of(supplier);
}

@Override
public <T> T get(SampleSpec<T> spec) {
return context.get().get(spec);
}

@Override
public <T> Sample<T> createNew(SampleSpec<T> spec) {
return context.get().createNew(spec);
}
private static final int BASE36 = 36;
private static final int MIN = 60_466_176;
private static final Random RANDOM = new Random();

@Override
public void close() {
if (context.isEvaluated()) {
context.get().close();
}
public CharSequence get() {
int calc = RANDOM.nextInt(Integer.MAX_VALUE - MIN) + MIN;
return Integer.toString(calc, BASE36);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package pl.wavesoftware.plugs.tools.packager.sample;

/**
* Defines a spec for a sample
*
* @param <T> a type of a sample
*/
public interface SampleSpec<T> {
import java.util.Random;
import java.util.function.Supplier;

public interface RandomSource extends Supplier<Random> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019 Wave Software
*
* Licensed 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 pl.wavesoftware.plugs.tools.packager.sample;

import io.vavr.Lazy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.zip.CRC32;

final class RandomSourceImpl implements RandomSource {
private static final Logger LOGGER =
LoggerFactory.getLogger(RandomSourceImpl.class);
private static final String SAMPLER_SEED_PROPERTY = "sampler.seed";

private final RandomSeedSupplier seedSupplier = new RandomSeedSupplier();
private final Environment environment;
private final Lazy<Random> randomLazy = Lazy.of(this::doGet);

RandomSourceImpl(Environment environment) {
this.environment = environment;
}

@Override
public Random get() {
return randomLazy.get();
}

private Random doGet() {
String seed = environment.getProperty(
SAMPLER_SEED_PROPERTY,
seedSupplier.get().toString()
);
CRC32 crc32 = new CRC32();
crc32.update(seed.getBytes(StandardCharsets.UTF_8));
long randomSeed = crc32.getValue();
LOGGER.info(
"Using seed: {}. To re-execute same exact case, set " +
"environmental variable `export SAMPLER_SEED={}` or Java's " +
"property `-D{}={}` with that seed value.",
seed,
seed,
SAMPLER_SEED_PROPERTY, seed
);
return new Random(randomSeed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,39 @@

package pl.wavesoftware.plugs.tools.packager.sample;

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Resolves a sample of a given spec.
*
* @param <T> a type of a sample
*/
public interface Sampler<T> {

/**
* Whether this sampler supports a given spec of a sample
*
* @return a sample spec
*/
boolean supports(SampleSpec<T> spec);
String SAMPLE_SCOPE = "sample";

/**
* Creates a sample based on a spec
*
* @return a created sample
* Creates a new sample
*/
Sample<T> create(SampleSpec<T> spec);
T create();

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@org.springframework.stereotype.Component
@interface Component {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = org.springframework.stereotype.Component.class)
String value() default "";
}
}

This file was deleted.

Loading

0 comments on commit e13a0be

Please sign in to comment.