forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NacosAutoRefreshPropertySourceLoaderTest.java
91 lines (80 loc) · 3.48 KB
/
NacosAutoRefreshPropertySourceLoaderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.sys.env;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.ByteUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import com.alibaba.nacos.sys.file.FileChangeEvent;
import com.alibaba.nacos.sys.file.FileWatcher;
import com.alibaba.nacos.sys.file.WatchFileCenter;
import com.alibaba.nacos.sys.utils.DiskUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NacosAutoRefreshPropertySourceLoaderTest.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class NacosAutoRefreshPropertySourceLoaderTest {
@Autowired
private ConfigurableEnvironment environment;
private static String oldConfPath = "";
@BeforeClass
public static void before() throws URISyntaxException {
oldConfPath = EnvUtil.getConfPath();
EnvUtil.setConfPath(new File(ClassLoader.getSystemResource("application.properties").toURI()).getParent());
}
@AfterClass
public static void after() {
EnvUtil.setConfPath(oldConfPath);
}
@Test
public void testConfigFileAutoRefresh() throws URISyntaxException, InterruptedException, NacosException, IOException {
final URL url = ClassLoader.getSystemResource("application.properties");
EnvUtil.setContextPath(url.getPath());
final String val1 = environment.getProperty("name");
Assert.assertEquals("test-1", val1);
final File file = new File(url.toURI());
final String newKey = "nacos.config.refresh-" + System.currentTimeMillis();
final String newVal = System.currentTimeMillis() + "-lessspring";
DiskUtils.writeFile(file, ByteUtils.toBytes("\n" + newKey + "=" + newVal), true);
CountDownLatch latch = new CountDownLatch(1);
WatchFileCenter.registerWatcher(EnvUtil.getConfPath(), new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
latch.countDown();
}
@Override
public boolean interest(String context) {
return StringUtils.contains(context, "application.properties");
}
});
latch.await();
ThreadUtils.sleep(10_000);
final String val2 = environment.getProperty(newKey);
Assert.assertEquals(newVal, val2);
}
}