/
CommandInternalsTest.java
149 lines (122 loc) · 4.95 KB
/
CommandInternalsTest.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 2011 - Will Glozer. All rights reserved.
package com.lambdaworks.redis;
import static com.lambdaworks.redis.protocol.LettuceCharsets.*;
import static org.assertj.core.api.Assertions.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import com.lambdaworks.redis.codec.RedisCodec;
import com.lambdaworks.redis.codec.Utf8StringCodec;
import com.lambdaworks.redis.output.NestedMultiOutput;
import com.lambdaworks.redis.output.StatusOutput;
import com.lambdaworks.redis.protocol.*;
public class CommandInternalsTest {
protected RedisCodec<String, String> codec = new Utf8StringCodec();
protected Command<String, String, String> command;
@Before
public final void createCommand() throws Exception {
CommandOutput<String, String, String> output = new StatusOutput<String, String>(codec);
command = new Command<String, String, String>(CommandType.INFO, output, null, false);
}
@Test
public void isCancelled() throws Exception {
assertThat(command.isCancelled()).isFalse();
assertThat(command.cancel(true)).isTrue();
assertThat(command.isCancelled()).isTrue();
assertThat(command.cancel(true)).isFalse();
}
@Test
public void isDone() throws Exception {
assertThat(command.isDone()).isFalse();
command.complete();
assertThat(command.isDone()).isTrue();
}
@Test
public void get() throws Exception {
command.getOutput().set(buffer("one"));
command.complete();
assertThat(command.get()).isEqualTo("one");
command.getOutput().toString();
}
@Test
public void customKeyword() throws Exception {
command = new Command<String, String, String>(MyKeywords.DUMMY, null, null, false);
command.setOutput(new StatusOutput<String, String>(codec));
assertThat(command.toString()).contains(MyKeywords.DUMMY.name());
}
@Test
public void customKeywordWithArgs() throws Exception {
command = new Command<String, String, String>(MyKeywords.DUMMY, null, new CommandArgs<String, String>(codec), false);
command.getArgs().add(MyKeywords.DUMMY);
assertThat(command.getArgs().toString()).contains(MyKeywords.DUMMY.name());
assertThat(command.getArgs().getKeywords()).contains(MyKeywords.DUMMY);
}
@Test
public void getWithTimeout() throws Exception {
command.getOutput().set(buffer("one"));
command.complete();
assertThat(command.get(0, TimeUnit.MILLISECONDS)).isEqualTo("one");
}
@Test(expected = TimeoutException.class, timeout = 100)
public void getTimeout() throws Exception {
assertThat(command.get(2, TimeUnit.MICROSECONDS)).isNull();
}
@Test(timeout = 100)
public void awaitTimeout() throws Exception {
assertThat(command.await(2, TimeUnit.MICROSECONDS)).isFalse();
}
@Test(expected = RedisCommandInterruptedException.class, timeout = 100)
public void getInterrupted() throws Exception {
Thread.currentThread().interrupt();
command.get();
}
@Test(expected = RedisCommandInterruptedException.class, timeout = 100)
public void getInterrupted2() throws Exception {
Thread.currentThread().interrupt();
command.get(5, TimeUnit.MILLISECONDS);
}
@Test(expected = RedisCommandInterruptedException.class, timeout = 100)
public void awaitInterrupted2() throws Exception {
Thread.currentThread().interrupt();
command.await(5, TimeUnit.MILLISECONDS);
}
@Test(expected = IllegalStateException.class)
public void outputSubclassOverride1() {
CommandOutput<String, String, String> output = new CommandOutput<String, String, String>(codec, null) {
@Override
public String get() throws RedisException {
return null;
}
};
output.set(null);
}
@Test(expected = IllegalStateException.class)
public void outputSubclassOverride2() {
CommandOutput<String, String, String> output = new CommandOutput<String, String, String>(codec, null) {
@Override
public String get() throws RedisException {
return null;
}
};
output.set(0);
}
@Test
public void nestedMultiError() throws Exception {
NestedMultiOutput<String, String> output = new NestedMultiOutput<String, String>(codec);
output.setError(buffer("Oops!"));
assertThat(output.getError()).isNotNull();
}
@Test
public void sillyTestsForEmmaCoverage() throws Exception {
assertThat(CommandType.valueOf("APPEND")).isEqualTo(CommandType.APPEND);
assertThat(CommandKeyword.valueOf("AFTER")).isEqualTo(CommandKeyword.AFTER);
}
private enum MyKeywords implements ProtocolKeyword {
DUMMY;
@Override
public byte[] getBytes() {
return name().getBytes();
}
}
}