Skip to content

Commit

Permalink
Trying to fix connection name parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefán J. Sigurðarson committed Mar 10, 2021
1 parent 9b4a070 commit 272be01
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
30 changes: 18 additions & 12 deletions projects/Unit/RabbitMQCtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static void Unblock()
ExecRabbitMQCtl("set_vm_memory_high_watermark 0.4");
}

private static readonly Regex s_getConnectionName = new Regex(@"\{""connection_name"",""(?<connection_name>[^""]+)""\}");
private static readonly Regex s_getConnectionProperties = new Regex(@"(?<pid>.*)\s\[.*\""connection_name\"",\""(?<connection_name>.*?)\"".*\]", RegexOptions.Compiled);
public class ConnectionInfo
{
public string Pid
Expand All @@ -252,6 +252,7 @@ public override string ToString()
return $"pid = {Pid}, name: {Name}";
}
}

public static List<ConnectionInfo> ListConnections()
{
Process proc = ExecRabbitMQCtl("list_connections --silent pid client_properties");
Expand All @@ -260,18 +261,23 @@ public static List<ConnectionInfo> ListConnections()
try
{
// {Environment.NewLine} is not sufficient
string[] splitOn = new string[] { "\r\n", "\n" };
string[] lines = stdout.Split(splitOn, StringSplitOptions.RemoveEmptyEntries);
// line: <rabbit@mercurio.1.11491.0> {.../*client_properties*/...}
return lines.Select(s =>
var matches = s_getConnectionProperties.Matches(stdout);
if (matches.Count > 0)
{
string[] columns = s.Split('\t');
Debug.Assert(!string.IsNullOrEmpty(columns[0]), "columns[0] is null or empty!");
Debug.Assert(!string.IsNullOrEmpty(columns[1]), "columns[1] is null or empty!");
Match match = s_getConnectionName.Match(columns[1]);
Debug.Assert(match.Success, "columns[1] is not in expected format.");
return new ConnectionInfo(columns[0], match.Groups["connection_name"].Value);
}).ToList();
var list = new List<ConnectionInfo>(matches.Count);
for (int i = 0; i < matches.Count; i++)
{
var s = matches[i];
Debug.Assert(s.Success, "Unable to parse connection list.");
Debug.Assert(s.Groups.ContainsKey("pid"), "Unable to parse pid from {stdout}");
Debug.Assert(s.Groups.ContainsKey("connection_name"), "Unable to parse connection_name from {stdout}");
list.Add(new ConnectionInfo(s.Groups["pid"].Value, s.Groups["connection_name"].Value));
}

return list;
}

return null;
}
catch (Exception)
{
Expand Down
10 changes: 5 additions & 5 deletions projects/Unit/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public async Task TestConfirmBarrier()
_model.ConfirmSelect();
for (int i = 0; i < 10; i++)
{
_model.BasicPublish("", string.Empty, null, new byte[] {});
_model.BasicPublish("", string.Empty, null, new byte[] { });
}
Assert.True(await _model.WaitForConfirmsAsync().ConfigureAwait(false));
}

[Fact]
public void TestConfirmBeforeWait()
public async Task TestConfirmBeforeWait()
{
Assert.ThrowsAsync<InvalidOperationException>(async () => await _model.WaitForConfirmsAsync());
await Assert.ThrowsAsync<InvalidOperationException>(async () => await _model.WaitForConfirmsAsync());
}

[Fact]
Expand All @@ -69,12 +69,12 @@ public async Task TestExchangeBinding()
_model.ExchangeBind("dest", "src", string.Empty);
_model.QueueBind(queue, "dest", string.Empty);

_model.BasicPublish("src", string.Empty, null, new byte[] {});
_model.BasicPublish("src", string.Empty, null, new byte[] { });
await _model.WaitForConfirmsAsync().ConfigureAwait(false);
Assert.NotNull(_model.BasicGet(queue, true));

_model.ExchangeUnbind("dest", "src", string.Empty);
_model.BasicPublish("src", string.Empty, null, new byte[] {});
_model.BasicPublish("src", string.Empty, null, new byte[] { });
await _model.WaitForConfirmsAsync().ConfigureAwait(false);
Assert.Null(_model.BasicGet(queue, true));

Expand Down

0 comments on commit 272be01

Please sign in to comment.