Skip to content

[TEST] Add mock plugins service loadServiceProviders#88690

Merged
grcevski merged 3 commits intoelastic:masterfrom
grcevski:operator/mock_plugins
Jul 22, 2022
Merged

[TEST] Add mock plugins service loadServiceProviders#88690
grcevski merged 3 commits intoelastic:masterfrom
grcevski:operator/mock_plugins

Conversation

@grcevski
Copy link
Copy Markdown
Contributor

This PR adds a bit of infrastructure to be able to correctly load service providers with our SPIClassIterator in MockNode. Namely, the mock node loads all plugins under the same classloader in tests, therefore we need special logic to find which is the correct plugin to use when constructing SPIs with a single argument constructor.

@grcevski grcevski added >test Issues or PRs that are addressing/adding tests :Core/Infra/Core Core issues without another label Team:Core/Infra Meta label for core/infra team v8.4.0 labels Jul 21, 2022
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@grcevski grcevski mentioned this pull request Jul 21, 2022
Copy link
Copy Markdown
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, a couple minor comments.

.instance();

// We shouldn't find the FooTestService implementation with PluginOther
assertEquals(List.of(), MockPluginsService.createExtensions(TestService.class, othPlugin));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that using the assertThat with hamcrest matchers produces much nicer failure messages for things like this. For example, here I think it would be:

assertThat(MockPluginsService.createExtensions(TestService.class, othPlugin), empty());


// We should find the FooTestService implementation when we use FooPlugin, because it matches the constructor arg.
var providers = MockPluginsService.createExtensions(TestService.class, fooPlugin);
assertEquals(1, providers.size());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above comment about hamcrest, I think both this and the next assert can be one statement with hamcrest, asserting the contents of the list:

assertThat(providers, allOf(hasSize(1), everyItem(instanceOf(BarTestService.class))));

result.addAll(createExtensions(service, pluginTuple.instance()));
}

return result.stream().toList();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be List.copyOf(result)


@SuppressWarnings("unchecked")
Constructor<T>[] constructors = (Constructor<T>[]) extensionClass.getConstructors();
boolean compatible = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this leniency?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there's no constructor argument we simply fall though to load the plugin, there's no need to distinguish from which Plugin the load happened. We ensure we de-dup in the caller method by using a set, because the service provider will get loaded for every plugin.

I'll add a comment to this effect.

@grcevski grcevski merged commit 5ffe4f0 into elastic:master Jul 22, 2022
@grcevski grcevski deleted the operator/mock_plugins branch July 22, 2022 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Core Core issues without another label Team:Core/Infra Meta label for core/infra team >test Issues or PRs that are addressing/adding tests v8.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants