<commit_before>﻿using System;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Xunit;
using Xunit.Abstractions;

namespace RabbitHarness.Tests
{
	public class Scratchpad
	{
		private readonly ITestOutputHelper _output;

		public Scratchpad(ITestOutputHelper output)
		{
			_output = output;
		}

		[Fact]
		public void When_testing_something()
		{
			var factory = new ConnectionFactory { HostName = "192.168.99.100" };

			using (var connection = factory.CreateConnection())
			using (var model = connection.CreateModel())
			{
				model.QueueDeclare("some queue", true, false, false, null);
				model.BasicQos(0, 1, false);
			}

			var message = new
			{
				Message = "message"
			};

			var reset = new AutoResetEvent(false);

			factory.Query<string>("some queue", message, response =>
			{
				_output.WriteLine(response);
				reset.Set();
			});

			reset.WaitOne();

			using (var connection = factory.CreateConnection())
			using (var model = connection.CreateModel())
			{
				model.QueueDelete("some queue");
			}
		}
	}

	public static class Extensions
	{
		public static void Query<TResponse>(this ConnectionFactory factory, string queueName, object message, Action<TResponse> callback)
		{
			var connection = factory.CreateConnection();
			var channel = connection.CreateModel();

			var correlationID = Guid.NewGuid().ToString();
			var replyTo = channel.QueueDeclare().QueueName;

			var listener = new EventingBasicConsumer(channel);
			channel.BasicConsume(replyTo, true, listener);
			listener.Received += (s, e) =>
			{
				if (e.BasicProperties.CorrelationId != correlationID)
					return;

				try
				{
					callback(MessageFrom<TResponse>(e.Body));
				}
				finally
				{
					channel.Dispose();
					connection.Dispose();<commit_msg>tests: fix basic test to reply to messages<commit_after>﻿using System;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Xunit;
using Xunit.Abstractions;

namespace RabbitHarness.Tests
{
	public class Scratchpad
	{
		public const string Host = "192.168.99.100";
		private readonly ITestOutputHelper _output;

		public Scratchpad(ITestOutputHelper output)
		{
			_output = output;
		}

		[RequiresRabbitFact(Host)]
		public void When_testing_something()
		{
			var factory = new ConnectionFactory { HostName = Host };

			using (var connection = factory.CreateConnection())
			using (var channel = connection.CreateModel())
			{
				channel.QueueDeclare("some queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
				channel.BasicQos(0, 1, false);

				var listener = new EventingBasicConsumer(channel);
				listener.Received += (s, e) =>
				{
					var result = Encoding.UTF8.GetString(e.Body).Length.ToString();

					var props = channel.CreateBasicProperties();
					props.CorrelationId = e.BasicProperties.CorrelationId;

					channel.BasicPublish(
						exchange: "",
						routingKey: e.BasicProperties.ReplyTo,
						basicProperties: props,
						body: Encoding.UTF8.GetBytes(result));
					channel.BasicAck(e.DeliveryTag, false);
				};

				channel.BasicConsume("some queue", false, listener);


				var message = new
				{
					Message = "message"
				};

				var reset = new AutoResetEvent(false);

				factory.Query<string>("some queue", message, response =>
				{
					_output.WriteLine(response);
					reset.Set();
				});

				reset.WaitOne();

				channel.QueueDelete("some queue");
			}
		}
	}

	public static class Extensions
	{
		public static void Query<TResponse>(this ConnectionFactory factory, string queueName, object message, Action<TResponse> callback)
		{
			var connection = factory.CreateConnection();
			var channel = connection.CreateModel();

			var correlationID = Guid.NewGuid().ToString();
			var replyTo = channel.QueueDeclare().QueueName;

			var listener = new EventingBasicConsumer(channel);
			channel.BasicConsume(replyTo, true, listener);
			listener.Received += (s, e) =>
			{
				if (e.BasicProperties.CorrelationId != correlationID)
					return;

				try
				{
					callback(MessageFrom<TResponse>(e.Body));
				}
				finally
				{
					channel.Dispose();
					connection.Dispose();
{'fraction_non_alphanumeric': 0.11494252873563218, 'fraction_numerical': 0.006568144499178982, 'mean_word_length': 3.6601092896174863, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532631', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1247, 'n_words': 296}