使用Guice创建要与ThreadWeaver一起使用的组件



我一直在开发的应用程序变得越来越复杂,已经到了我在并发方面一次又一次遇到同样问题的地步。解决同样的问题而不进行任何回归测试已经没有任何意义了。

那是我找到ThreadWeaver的时候。对于我编写的一些简单的并发案例来说,这真的很好,但当我试图用我的生产代码处理一些更复杂的案例时,我开始感到沮丧。特别是在使用Guice注入组件时。

我很难理解ThreadWeaver运行测试的方式的含义,也很难在wiki文档中找到任何关于Guice或DI的内容,但没有成功。

Guice与ThreadWeaver兼容吗?

这是我的测试

@Test
public void concurrency_test() {
    AnnotatedTestRunner runner = new AnnotatedTestRunner();
    runner.runTests(OPYLWeaverImpl.class, OPYLSurrogateTranscodingService.class);
}

这是我的测试实现

public class OPYLWeaverImpl extends WeaverFixtureBase {
@Inject private TaskExecutor                 taskExecutor;
@Inject private Serializer                   serializer;
@Inject private CountingObjectFileMarshaller liveFileMarshaller;
@Inject private GraphModel                   graphModel;
@Inject private CountingModelUpdaterService  updaterService;
@Inject private BabelCompiler                babelCompiler;
@Inject private EventBus                     eventBus;
OPYLSurrogateTranscodingService service;
private Path testPath;
@ThreadedBefore
public void before() {
    service = new OPYLSurrogateTranscodingService(eventBus, taskExecutor, serializer, liveFileMarshaller,
            () -> new OPYLSurrogateTranscodingService.Importer(graphModel, babelCompiler, updaterService, eventBus),
            () -> new OPYLSurrogateTranscodingService.Validator(eventBus, babelCompiler),
            () -> new OPYLSurrogateTranscodingService.Exporter(graphModel, updaterService));
}
@ThreadedMain
public void mainThread() {
    testPath = FilePathOf.OASIS.resolve("Samples/fake-powershell-unit-test.opyl");
    service.applyToExistingGraphModel(testPath);
}
@ThreadedSecondary
public void secondaryThread() {
}
@ThreadedAfter
public void after() {
}

WeaverFixtureBase

public class WeaverFixtureBase {
@Inject protected CountingEventBus eventBus;
@Before public final void setupComponents() {
    Injector injector = Guice.createInjector(new WeaverTestingEnvironmentModule(CommonSerializationBootstrapper.class));
    injector.getMembersInjector((Class) this.getClass()).injectMembers(this);
}
private class WeaverTestingEnvironmentModule extends AbstractModule {
    private final Class<? extends SerializationBootstrapper> serializationBootstrapper;
    public WeaverTestingEnvironmentModule(Class<? extends SerializationBootstrapper> serializationConfiguration) {
        serializationBootstrapper = serializationConfiguration;
    }
    @Override protected void configure() {
        bind(TaskExecutor.class).to(FakeSerialTaskExecutor.class);
        bind(SerializationBootstrapper.class).to(serializationBootstrapper);
        bind(ModelUpdaterService.class).toInstance(new CountingModelUpdaterService());
        bindFactory(StaticSerializationConfiguration.Factory.class);
        CountingEventBus localEventBus = new CountingEventBus();
        bind(Key.get(EventBus.class, Bindings.GlobalEventBus.class)).toInstance(localEventBus);
        bind(Key.get(EventBus.class, Bindings.LocalEventBus.class)).toInstance(localEventBus);
        bind(CountingEventBus.class).toInstance(localEventBus);
        bind(EventBus.class).toInstance(localEventBus);
    }
    @Provides
    @Singleton
    public GraphModel getGraphModel(EventBus eventBus, Serializer serializer) {
        return MockitoUtilities.createMockAsInterceptorTo(new GraphModel(eventBus, serializer));
    }
}

但是,当类加载器加载OPYLWeaverImpl时,Guice的东西都不会消失,我得到了一大堆null。

我觉得这是一种"错过了一些非常简单的事情"的场景。对不起,如果是的话!

上面的评论是对的。Threadweaver完全不知道JUnit。Threadweaver是它自己的运行程序,它根据自己的注释执行测试用例。您不能在ThreadWeaver测试中使用任何JUnit特定的注释。

除此之外,Thread Weaver不需要任何特定框架的兼容性。它处理Java字节码,并使用aeperate类加载器加载被处理的代码。

最后,没有任何二次测试的Thread Weaver测试没有任何意义。线程编织器通过交错不同的执行路径来工作。在没有第二个线程的情况下,thread Weaver只遍历一个线程而不添加任何值。

最新更新