Skip to content

Commit 519c9c2

Browse files
DOC-4560 trans pipe examples (#418)
* DOC-4560 started trans/pipe examples * DOC-4560 added testable trans/pipe examples
1 parent 410ddd6 commit 519c9c2

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/Doc/PipeTransExample.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// EXAMPLE: pipe_trans_tutorial
2+
using NRedisStack;
3+
using StackExchange.Redis;
4+
//REMOVE_START
5+
using NRedisStack.Tests;
6+
7+
namespace Doc;
8+
[Collection("DocsTests")]
9+
//REMOVE_END
10+
public class PipeTransExample
11+
// REMOVE_START
12+
: AbstractNRedisStackTest, IDisposable
13+
// REMOVE_END
14+
{
15+
// REMOVE_START
16+
public PipeTransExample(EndpointsFixture fixture) : base(fixture) { }
17+
18+
[SkippableFact]
19+
// REMOVE_END
20+
public async Task run()
21+
{
22+
//REMOVE_START
23+
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
24+
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
25+
var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
26+
//REMOVE_END
27+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
28+
var db = muxer.GetDatabase();
29+
// REMOVE_START
30+
db.KeyDelete(new RedisKey[] {
31+
"counter:1", "counter:2", "counter:3",
32+
"seat:0", "seat:1", "seat:2", "seat:3", "seat:4",
33+
"customer:39182",
34+
"Details"
35+
});
36+
// REMOVE_END
37+
38+
// STEP_START basic_pipe
39+
var pipeline = new Pipeline(db);
40+
41+
for (int i = 0; i < 5; i++)
42+
{
43+
pipeline.Db.StringSetAsync($"seat:{i}", $"#{i}");
44+
}
45+
pipeline.Execute();
46+
47+
var resp1 = db.StringGet("seat:0");
48+
Console.WriteLine(resp1); // >>> #0
49+
50+
var resp2 = db.StringGet("seat:3");
51+
Console.WriteLine(resp2); // >>> #3
52+
53+
var resp3 = db.StringGet("seat:4");
54+
Console.WriteLine(resp2); // >>> #4
55+
// STEP_END
56+
// REMOVE_START
57+
Assert.Equal("#0", resp1);
58+
Assert.Equal("#3", resp2);
59+
Assert.Equal("#4", resp3);
60+
// REMOVE_END
61+
62+
// STEP_START basic_trans
63+
var trans = new Transaction(db);
64+
65+
trans.Db.StringIncrementAsync("counter:1", 1);
66+
trans.Db.StringIncrementAsync("counter:2", 2);
67+
trans.Db.StringIncrementAsync("counter:3", 3);
68+
69+
trans.Execute();
70+
71+
var resp4 = db.StringGet("counter:1");
72+
Console.WriteLine(resp4); // >>> 1
73+
74+
var resp5 = db.StringGet("counter:2");
75+
Console.WriteLine(resp5); // >>> 2
76+
77+
var resp6 = db.StringGet("counter:3");
78+
Console.WriteLine(resp6); // >>> 3
79+
// STEP_END
80+
// REMOVE_START
81+
Assert.Equal("1", resp4);
82+
Assert.Equal("2", resp5);
83+
Assert.Equal("3", resp6);
84+
// REMOVE_END
85+
86+
// STEP_START trans_watch
87+
var watchedTrans = new Transaction(db);
88+
89+
watchedTrans.AddCondition(Condition.KeyNotExists("customer:39182"));
90+
91+
watchedTrans.Db.HashSetAsync(
92+
"customer:39182",
93+
new HashEntry[]{
94+
new HashEntry("name", "David"),
95+
new HashEntry("age", "27")
96+
}
97+
);
98+
99+
bool succeeded = watchedTrans.Execute();
100+
Console.WriteLine(succeeded); // >>> true
101+
// STEP_END
102+
// REMOVE_START
103+
Assert.True(succeeded);
104+
// REMOVE_END
105+
106+
// STEP_START when_condition
107+
bool resp7 = db.HashSet("Details", "SerialNumber", "12345");
108+
Console.WriteLine(resp7); // >>> true
109+
110+
db.HashSet("Details", "SerialNumber", "12345A", When.NotExists);
111+
string resp8 = db.HashGet("Details", "SerialNumber");
112+
Console.WriteLine(resp8); // >>> 12345
113+
114+
db.HashSet("Details", "SerialNumber", "12345A");
115+
string resp9 = db.HashGet("Details", "SerialNumber");
116+
Console.WriteLine(resp9); // >>> 12345A
117+
// STEP_END
118+
// REMOVE_START
119+
Assert.True(resp7);
120+
Assert.Equal("12345", resp8);
121+
Assert.Equal("12345A", resp9);
122+
// REMOVE_END
123+
}
124+
}

0 commit comments

Comments
 (0)