forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNotificationTests.cs
More file actions
230 lines (200 loc) · 8.85 KB
/
NotificationTests.cs
File metadata and controls
230 lines (200 loc) · 8.85 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using NUnit.Framework;
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using static Npgsql.Tests.TestUtil;
namespace Npgsql.Tests;
public class NotificationTests : TestBase
{
[Test, Description("Simple LISTEN/NOTIFY scenario")]
public void Notification()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
using var conn = OpenConnection();
var receivedNotification = false;
conn.ExecuteNonQuery($"LISTEN {notify}");
conn.Notification += (o, e) => receivedNotification = true;
conn.ExecuteNonQuery($"NOTIFY {notify}");
Assert.That(receivedNotification);
}
[Test, Description("Generates a notification that arrives after reader data that is already being read")]
[IssueLink("https://github.com/npgsql/npgsql/issues/252")]
public async Task Notification_after_data()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
var receivedNotification = false;
using var conn = OpenConnection();
using var cmd = conn.CreateCommand();
cmd.CommandText = $"LISTEN {notify}";
cmd.ExecuteNonQuery();
conn.Notification += (o, e) => receivedNotification = true;
cmd.CommandText = "SELECT generate_series(1,10000)";
using (var reader = cmd.ExecuteReader())
{
//After "notify notifytest1", a notification message will be sent to client,
//And so the notification message will stick with the last response message of "select generate_series(1,10000)" in Npgsql's tcp receiving buffer.
using (var conn2 = new NpgsqlConnection(ConnectionString))
{
conn2.Open();
using (var command = conn2.CreateCommand())
{
command.CommandText = $"NOTIFY {notify}";
command.ExecuteNonQuery();
}
}
// Allow some time for the notification to get delivered
await Task.Delay(2000);
Assert.That(reader.Read());
Assert.That(reader.GetValue(0), Is.EqualTo(1));
}
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
Assert.That(receivedNotification);
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/1024")]
public void Wait()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
using var conn = OpenConnection();
using var notifyingConn = OpenConnection();
var receivedNotification = false;
conn.ExecuteNonQuery($"LISTEN {notify}");
notifyingConn.ExecuteNonQuery($"NOTIFY {notify}");
conn.Notification += (o, e) => receivedNotification = true;
Assert.That(conn.Wait(0), Is.EqualTo(true));
Assert.That(receivedNotification);
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/1024")]
public void Wait_with_timeout()
{
using var conn = OpenConnection();
Assert.That(conn.Wait(100), Is.EqualTo(false));
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
[Test]
public void Wait_with_prepended_message()
{
using var dataSource = CreateDataSource();
using (dataSource.OpenConnection()) {} // A DISCARD ALL is now prepended in the connection's write buffer
using var conn = dataSource.OpenConnection();
Assert.That(conn.Wait(100), Is.EqualTo(false));
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/1024")]
public async Task WaitAsync()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
await using var conn = await OpenConnectionAsync();
await using var notifyingConn = await OpenConnectionAsync();
var receivedNotification = false;
await conn.ExecuteNonQueryAsync($"LISTEN {notify}");
await notifyingConn.ExecuteNonQueryAsync($"NOTIFY {notify}");
conn.Notification += (o, e) => receivedNotification = true;
await conn.WaitAsync(0);
Assert.That(receivedNotification);
Assert.That(await conn.ExecuteScalarAsync("SELECT 1"), Is.EqualTo(1));
}
[Test]
public void WaitAsync_with_timeout()
{
using var conn = OpenConnection();
Assert.That(async () => await conn.WaitAsync(100), Is.EqualTo(false));
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
[Test]
public void Wait_with_keepalive()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
using var dataSource = CreateDataSource(csb =>
{
csb.KeepAlive = 1;
csb.Pooling = false;
});
using var conn = dataSource.OpenConnection();
using var notifyingConn = dataSource.OpenConnection();
conn.ExecuteNonQuery($"LISTEN {notify}");
var notificationTask = Task.Delay(2000).ContinueWith(t => notifyingConn.ExecuteNonQuery($"NOTIFY {notify}"));
conn.Wait();
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
// A safeguard against closing an active connection
notificationTask.GetAwaiter().GetResult();
//Assert.That(TestLoggerSink.Records, Has.Some.With.Property("EventId").EqualTo(new EventId(NpgsqlEventId.Keepalive)));
}
[Test]
public async Task WaitAsync_with_keepalive()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
await using var dataSource = CreateDataSource(csb =>
{
csb.KeepAlive = 1;
csb.Pooling = false;
});
await using var conn = await dataSource.OpenConnectionAsync();
await using var notifyingConn = await dataSource.OpenConnectionAsync();
await conn.ExecuteNonQueryAsync($"LISTEN {notify}");
var notificationTask = Task.Delay(2000).ContinueWith(t => notifyingConn.ExecuteNonQuery($"NOTIFY {notify}"));
await conn.WaitAsync();
//Assert.That(TestLoggerSink.Records, Has.Some.With.Property("EventId").EqualTo(new EventId(NpgsqlEventId.Keepalive)));
Assert.That(await conn.ExecuteScalarAsync("SELECT 1"), Is.EqualTo(1));
// A safeguard against closing an active connection
await notificationTask;
}
[Test]
public void WaitAsync_cancellation()
{
var notify = GetUniqueIdentifier(nameof(NotificationTests));
using (var conn = OpenConnection())
{
Assert.ThrowsAsync<OperationCanceledException>(async () => await conn.WaitAsync(new CancellationToken(true)));
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
using (var conn = OpenConnection())
{
conn.ExecuteNonQuery($"LISTEN {notify}");
var cts = new CancellationTokenSource(1000);
Assert.ThrowsAsync<OperationCanceledException>(async () => await conn.WaitAsync(cts.Token));
Assert.That(conn.ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
}
[Test]
public void Wait_breaks_connection()
{
using var dataSource = CreateDataSource();
using var conn = dataSource.OpenConnection();
Task.Delay(1000).ContinueWith(t =>
{
using var conn2 = OpenConnection();
conn2.ExecuteNonQuery($"SELECT pg_terminate_backend({conn.ProcessID})");
});
var pgEx = Assert.Throws<PostgresException>(conn.Wait)!;
Assert.That(pgEx.SqlState, Is.EqualTo(PostgresErrorCodes.AdminShutdown));
Assert.That(conn.FullState, Is.EqualTo(ConnectionState.Broken));
}
[Test]
public void WaitAsync_breaks_connection()
{
using var dataSource = CreateDataSource();
using var conn = dataSource.OpenConnection();
Task.Delay(1000).ContinueWith(t =>
{
using var conn2 = OpenConnection();
conn2.ExecuteNonQuery($"SELECT pg_terminate_backend({conn.ProcessID})");
});
var pgEx = Assert.ThrowsAsync<PostgresException>(async () => await conn.WaitAsync())!;
Assert.That(pgEx.SqlState, Is.EqualTo(PostgresErrorCodes.AdminShutdown));
Assert.That(conn.FullState, Is.EqualTo(ConnectionState.Broken));
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4911")]
public async Task Big_notice_while_loading_types()
{
await using var adminConn = await OpenConnectionAsync();
// Max notification payload is 8000
await using var dataSource = CreateDataSource(csb => csb.ReadBufferSize = 4096);
await using var conn = await dataSource.OpenConnectionAsync();
var notify = GetUniqueIdentifier(nameof(Big_notice_while_loading_types));
await conn.ExecuteNonQueryAsync($"LISTEN {notify}");
var payload = new string('a', 5000);
await adminConn.ExecuteNonQueryAsync($"NOTIFY {notify}, '{payload}'");
await conn.ReloadTypesAsync();
}
}