Skip to content

Commit abe4fc9

Browse files
author
antonybi
committed
fix coverage
1 parent 7ea6f30 commit abe4fc9

File tree

8 files changed

+77
-36
lines changed

8 files changed

+77
-36
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<dependency>
3737
<groupId>io.github.antonybi</groupId>
3838
<artifactId>timechannel</artifactId>
39-
<version>1.1.0</version>
39+
<version>1.1.1</version>
4040
</dependency>
4141
```
4242

@@ -146,13 +146,16 @@ public class DemoService {
146146
# 查看channel的总数
147147
zcard space:0:expiryTime:channel
148148

149+
# 查看channel的租约过期时间
150+
zscore space:0:expiryTime:channel 0
151+
149152
# 查看目前可用的channel总数,时间戳换成当前时间
150153
zcount space:0:expiryTime:channel 0 1661079389000
151154

152155
# 查看正在被占用的channel
153156
zrangebyscore space:0:expiryTime:channel 1661079389000 9999999999999 WITHSCORES
154157

155-
# 根据上条命令查到的频道号查看最后一次申请日志
158+
# 根据上条命令查到的channel查看最后一次申请日志
156159
get space:0:channel:0:log
157160
```
158161

src/main/java/timechannel/core/Allocator.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import timechannel.exception.TimeChannelInternalException;
1111

1212
import javax.annotation.PostConstruct;
13+
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.nio.charset.StandardCharsets;
1516
import java.time.Duration;
@@ -60,13 +61,10 @@ public void init() {
6061

6162
private byte[] readFile(String filePath) {
6263
InputStream inputStream = this.getClass().getResourceAsStream("/" + filePath);
63-
if (inputStream == null) {
64-
log.error("cannot find lua script");
65-
System.exit(0);
66-
}
64+
Assert.notNull(inputStream, "cannot find lua script");
6765
try {
6866
return IOUtils.toByteArray(inputStream);
69-
} catch (Exception e) {
67+
} catch (IOException e) {
7068
log.error("cannot read " + filePath, e);
7169
System.exit(0);
7270
return new byte[0];
@@ -101,7 +99,11 @@ private Lease doGrant(int channelQuantity, Duration ttl, String appName) {
10199
throw new TimeChannelInternalException("no idle channel: " + Arrays.toString(response.toArray()));
102100
}
103101

104-
return new Lease(response.get(1), response.get(2), response.get(3));
102+
Lease lease = new Lease();
103+
lease.setChannel(response.get(1));
104+
lease.setEffectiveTime(response.get(2));
105+
lease.setExpiryTime(response.get(3));
106+
return lease;
105107
}
106108

107109
/**

src/main/java/timechannel/core/Generator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private long getLocalServerTime() {
244244

245245
public LocalDateTime parseDateTime(long guid) {
246246
return LocalDateTime.ofInstant(
247-
Instant.ofEpochMilli(guid >> (channelBits + sequenceBits)),
247+
Instant.ofEpochMilli(guid >> (groupId + channelBits + sequenceBits)),
248248
TimeZone.getDefault().toZoneId());
249249
}
250250

src/main/java/timechannel/core/Lease.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ void setExpiryTime(long expiryTime) {
4646
this.expiryTime = expiryTime;
4747
}
4848

49-
Lease(long channel, long effectiveTime, long expiryTime) {
50-
this.channel = channel;
51-
this.effectiveTime = effectiveTime;
52-
this.expiryTime = expiryTime;
53-
}
54-
5549
@Override
5650
public String toString() {
5751
return "Lease{" +

src/main/java/timechannel/exception/TimeChannelInternalException.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99
**/
1010
public class TimeChannelInternalException extends RuntimeException {
1111

12-
private final String message;
13-
1412
public TimeChannelInternalException(String message) {
1513
super(message);
16-
this.message = message;
17-
}
18-
19-
@Override
20-
public String getMessage() {
21-
return message;
2214
}
2315

2416
}

src/test/java/timechannel/GuidTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,19 @@ class GuidTest {
3535
@Resource
3636
private Guid guid;
3737

38-
@Disabled("very slow")
38+
@Disabled("调整sequence的配置,12bit可达到50w/s")
3939
@Test
4040
void benchmarkNextId() throws InterruptedException {
41-
Set<Long> rst = Collections.synchronizedSet(new HashSet<>());
4241
CountDownLatch c = new CountDownLatch(3);
4342

4443
Thread t1 = new Thread(() -> {
45-
consumeGuid(rst, c);
44+
consumeGuid(c);
4645
});
4746
Thread t2 = new Thread(() -> {
48-
consumeGuid(rst, c);
47+
consumeGuid(c);
4948
});
5049
Thread t3 = new Thread(() -> {
51-
consumeGuid(rst, c);
50+
consumeGuid(c);
5251
});
5352
t1.start();
5453
t2.start();
@@ -58,19 +57,26 @@ void benchmarkNextId() throws InterruptedException {
5857
log.info("done!");
5958
}
6059

61-
private void consumeGuid(Set<Long> rst, CountDownLatch c) {
60+
private void consumeGuid(CountDownLatch c) {
6261
for (int i = 0; i < 10000000; i++) {
63-
long id = guid.nextId();
64-
assertFalse(rst.contains(id));
65-
rst.add(id);
62+
assertDoesNotThrow(() -> guid.nextId());
6663
}
6764
c.countDown();
6865
}
6966

7067
@Test
7168
void nextId() {
72-
assertTrue(guid.nextId() > 0);
73-
assertTrue(guid.nextId() > 0);
69+
Set<Long> rst = Collections.synchronizedSet(new HashSet<>());
70+
long last = 0;
71+
72+
for (int i = 0; i < 100; i++) {
73+
// 加速消耗直到一个时间片內序号都耗尽
74+
long id = guid.nextId();
75+
assertTrue(id > last);
76+
assertFalse(rst.contains(id));
77+
rst.add(id);
78+
last = id;
79+
}
7480
}
7581

7682
@Test
@@ -80,7 +86,7 @@ void nextId2() {
8086

8187
@Test
8288
void parseDateTime() {
83-
assertEquals(LocalDateTime.parse("2075-04-23T11:50:00.727"), guid.parseDateTime(3484645589754769408L));
89+
assertEquals(LocalDateTime.parse("2022-08-28T00:22:33.251"), guid.parseDateTime(13611969357836288L));
8490
}
8591

8692
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package timechannel.core;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.ActiveProfiles;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit.jupiter.SpringExtension;
9+
import timechannel.Guid;
10+
import timechannel.TestConfig;
11+
import timechannel.exception.TimeChannelInternalException;
12+
13+
import javax.annotation.Resource;
14+
15+
import java.time.Duration;
16+
import java.time.temporal.TemporalUnit;
17+
import java.util.concurrent.TimeUnit;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
/**
22+
* @author antonybi
23+
* @since 2022/08/28
24+
*/
25+
@ExtendWith(SpringExtension.class)
26+
@SpringBootTest(classes = {Guid.class})
27+
@ContextConfiguration(classes = {TestConfig.class})
28+
@ActiveProfiles("test")
29+
class AllocatorTest {
30+
31+
@Resource
32+
private Allocator allocator;
33+
34+
@Test
35+
void grant() {
36+
for (int i = 0; i < 256 - 1; i++) {
37+
// 本应用启动已经占用一个
38+
allocator.grant(256, Duration.ofSeconds(15), "junit");
39+
}
40+
assertThrowsExactly(TimeChannelInternalException.class,
41+
() -> allocator.grant(256, Duration.ofSeconds(15), "junit"));
42+
}
43+
44+
}

src/test/resources/application-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ guid:
1010
bits:
1111
group: 1 # 频道分组bit位数,默认0
1212
channel: 8 # 频道bit位数,默认11位共2048个
13-
sequence: 12 # 序列号bit位数,默认10位即1024/ms
13+
sequence: 4 # 序列号bit位数,默认10位即1024/ms
1414
redis:
1515
host: 127.0.0.1
1616
port: 6379

0 commit comments

Comments
 (0)