Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolved embedded value in annotation @ApolloConfigChangeListener #3349

Conversation

Anilople
Copy link
Contributor

@Anilople Anilople commented Oct 23, 2020

What's the purpose of this PR

Make ApolloConfigChangeListener's value more dynamic.

Which issue(s) this PR fixes:

#3091

Brief changelog

The value in ApolloConfigChangeListener can be resolved now.

One can write like follow:

  @Configuration
  @EnableApolloConfig
  static class TestResolveExpressionFromSystemPropertyConfiguration {

    @ApolloConfigChangeListener(value = {ConfigConsts.NAMESPACE_APPLICATION,
        "${redis.namespace}"})
    private void onChange(ConfigChangeEvent event) {
    }
  }

"${redis.namespace}" will be resolved.

If there is property

redis.namespace=xxx

then "${redis.namespace}" will get xxx

If there is property

redis.namespace=yyy

then "${redis.namespace}" will get yyy


Also one can write code like follow:

  @Configuration
  @EnableApolloConfig
  static class TestResolveExpressionSimpleConfiguration {

    @ApolloConfigChangeListener("${simple.application:abc}")
    private void onChange(ConfigChangeEvent event) {
    }
  }

"${simple.application:abc}" will be resolved to abc if there is no property for key simple.application

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Read the Contributing Guide before making this pull request.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit tests to verify the code.
  • Run mvn clean test to make sure this pull request doesn't break anything.

@nobodyiam
Copy link
Member

Looks good! Will take a detailed look soon

@nobodyiam
Copy link
Member

@Anilople I'm thinking is it necessary to support embedded value for @EnableApolloConfig and @ApolloConfig?

@Anilople
Copy link
Contributor Author

Anilople commented Nov 1, 2020

@Anilople I'm thinking is it necessary to support embedded value for @EnableApolloConfig and @ApolloConfig?

Implement BeanFactoryAware for DefaultApolloConfigRegistrarHelper to process @EnableApolloConfig.

But I'm not sure it will work.

@Anilople
Copy link
Contributor Author

Anilople commented Nov 1, 2020

@Anilople I'm thinking is it necessary to support embedded value for @EnableApolloConfig and @ApolloConfig?

Implement BeanFactoryAware for DefaultApolloConfigRegistrarHelper to process @EnableApolloConfig.

But I'm not sure it will work.

Meet java.lang.NullPointerException... Let me try another way.

@codecov-io
Copy link

codecov-io commented Nov 2, 2020

Codecov Report

Merging #3349 (811f42a) into master (bd388e8) will increase coverage by 0.07%.
The diff coverage is 91.48%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3349      +/-   ##
============================================
+ Coverage     51.43%   51.51%   +0.07%     
- Complexity     2301     2307       +6     
============================================
  Files           442      441       -1     
  Lines         13741    13762      +21     
  Branches       1399     1400       +1     
============================================
+ Hits           7068     7089      +21     
  Misses         6185     6185              
  Partials        488      488              
Impacted Files Coverage Δ Complexity Δ
...i/DefaultConfigPropertySourcesProcessorHelper.java 100.00% <ø> (ø) 4.00 <0.00> (ø)
...o/spring/annotation/ApolloAnnotationProcessor.java 91.08% <89.18%> (-2.86%) 21.00 <13.00> (+12.00) ⬇️
...pollo/spring/annotation/ApolloConfigRegistrar.java 100.00% <100.00%> (ø) 3.00 <2.00> (+1.00)
...amework/apollo/spring/config/NamespaceHandler.java 86.95% <100.00%> (+1.95%) 3.00 <1.00> (ø)
...spring/spi/DefaultApolloConfigRegistrarHelper.java 100.00% <100.00%> (ø) 6.00 <3.00> (+3.00)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bd388e8...811f42a. Read the comment docs.

Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

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

Please see some comments below.
BTW is it possible to make it work for xml configuration? i.e. to resolve the namespace placeholder in com.ctrip.framework.apollo.spring.config.NamespaceHandler.BeanParser#doParse

Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

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

This version looks much better, but I still have some comments as below, please take a look.
BTW, I noticed you created many inner classes and xmls for the tests, however I think many of them are quite similar, so maybe we could try to reuse them instead of creating new ones.

assertEquals(someValue, configuration.getSomeKey());
verify(yyyConfig, times(1)).getProperty(eq(someKey), anyString());

System.clearProperty(propertyKey);
Copy link
Member

Choose a reason for hiding this comment

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

Please clear the state in tearDown method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use

  @After
  public void javaConfigAnnotationTestTearDown() {
    // clear the system property
    clearSystemPropertiesDefineWithStaticStringField(SystemPropertyKeyConstants.class);
  }

to do it.

Copy link
Member

Choose a reason for hiding this comment

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

This is a little complex, I think we could simply assign some variables and just use System.clearProperty(xxx) in tear down method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change to code like follow:

  /**
   * forbidden to override the method {@link super#tearDown()}.
   */
  @After
  public void javaConfigAnnotationTestTearDown() {
    // clear the system properties
    System.clearProperty(SystemPropertyKeyConstants.SIMPLE_NAMESPACE);
    System.clearProperty(SystemPropertyKeyConstants.REDIS_NAMESPACE);
    System.clearProperty(SystemPropertyKeyConstants.FROM_SYSTEM_NAMESPACE);
    System.clearProperty(SystemPropertyKeyConstants.FROM_SYSTEM_YAML_NAMESPACE);
    System.clearProperty(SystemPropertyKeyConstants.FROM_NAMESPACE_APPLICATION_KEY);
    System.clearProperty(SystemPropertyKeyConstants.FROM_NAMESPACE_APPLICATION_KEY_YAML);
  }

Clear system properties manually instead of using reflection.

// no using
verify(ignoreConfig, never()).addChangeListener(any(ConfigChangeListener.class));

// strange, it invoke 2 times
Copy link
Member

Choose a reason for hiding this comment

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

because there is an auto update feature enabled: com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor#initializeAutoUpdatePropertiesFeature

private void check(String xmlLocation, int expectedTimeout, int expectedBatch) {

@Test
public void testResolveNamespacesWithDefaultValue() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

this case failed in my machine, as there is no value for xxx.from.system.property

Copy link
Member

Choose a reason for hiding this comment

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

btw, this case looks like the same as testUnresolvedNamespaces

Copy link
Contributor Author

@Anilople Anilople Nov 22, 2020

Choose a reason for hiding this comment

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

Fixed it in commit b5eb6f0

@Anilople
Copy link
Contributor Author

In my machine(Win10), there is a test Failure in DefaultConfigTest.testGetIntPropertyMultipleTimesWithShortExpireTime:280.

Results :

Failed tests: 
  DefaultConfigTest.testGetIntPropertyMultipleTimesWithShortExpireTime:280 
properties.getProperty("someKey");
Wanted 2 times:
-> at com.ctrip.framework.apollo.internals.DefaultConfigTest.testGetIntPropertyMultipleTimesWithShortExpireTime(DefaultConfigTest.java:280)
But was 1 time:
-> at com.ctrip.framework.apollo.internals.DefaultConfig.getProperty(DefaultConfig.java:77)

Tests in error: 
  RemoteConfigRepositoryTest.testLongPollingRefresh:298 ? Timeout Waited 5000 mi...

Tests run: 293, Failures: 1, Errors: 1, Skipped: 0

I don't know why.

Maybe a bug in mockito? Or exists some concurrent problem between tests?

It seems ok on Linux platform.

@nobodyiam
Copy link
Member

In my machine(Win10), there is a test Failure in DefaultConfigTest.testGetIntPropertyMultipleTimesWithShortExpireTime:280.

Results :

Failed tests: 
  DefaultConfigTest.testGetIntPropertyMultipleTimesWithShortExpireTime:280 
properties.getProperty("someKey");
Wanted 2 times:
-> at com.ctrip.framework.apollo.internals.DefaultConfigTest.testGetIntPropertyMultipleTimesWithShortExpireTime(DefaultConfigTest.java:280)
But was 1 time:
-> at com.ctrip.framework.apollo.internals.DefaultConfig.getProperty(DefaultConfig.java:77)

Tests in error: 
  RemoteConfigRepositoryTest.testLongPollingRefresh:298 ? Timeout Waited 5000 mi...

Tests run: 293, Failures: 1, Errors: 1, Skipped: 0

I don't know why.

Maybe a bug in mockito? Or exists some concurrent problem between tests?

It seems ok on Linux platform.

This test was not well written, I'll fix it later.

Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/client apollo-client feature Categorizes issue as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants