Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
Messages not sent to Azure Service Bus in integration tests

I want to perform integration tests which involve interaction with service bus. I have noticed that some messages are not sent, even though no exception gets thrown. I am using a free tier of ASB.

That's the service method responsible for sending the message:

    public class ServiceBusMessagingSender<T> : IMessagingSender<T> where T : IMessage
    {

    private readonly string _connectionString = Environment.GetEnvironmentVariable("SERVICE_BUS_CONNECTION_STRING_SENDER");
    private readonly ServiceBusSender _sender;
    private readonly IMessageSerializer<T> _messageSerializer;

    public ServiceBusMessagingSender(string queueName, IMessageSerializer<T> messageSerializer, string connectionString=null)
    {
        _messageSerializer = messageSerializer;
        _connectionString ??= connectionString;
        var client = new ServiceBusClient(_connectionString);
        this._sender = client.CreateSender(queueName);
    }


    public void SendMessage(T message)
    {
        var serviceBusMessage = _messageSerializer.Serialize(message);
        _sender.SendMessageAsync(serviceBusMessage).GetAwaiter().GetResult();
    }

This method is then called in tests in NUnit suite:

    public class EndtoEndTests {

    private readonly ServiceBusMessagingSender<DataRequestDto> _messagingSender = new(RequestsQueue, new MessageSerializer<DataRequestDto>(), WriterConnectionString);


    [Test]
    public void GoodTypes()
    {
        //...
        
        //WHEN
        var request = new GoodTypesRequest()
        {
            RequestTimeStamp = DateTime.Now,
            RequestType = RequestType.GoodTypes
        };

        _messagingSender.SendMessage(request );
        ///...
    }
}

I am using the following versions of packages for ASB:

    <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.2.0" />
    <PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.1.3" />
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are calling a Async method inside a void so you are ending up in a race condition of if the send can complete before the function closes. Change your function to a Task rather than void and the problem should go away. You also might want to change _sender.SendMessageAsync(serviceBusMessage).GetAwaiter().GetResult(); to await _sender.SendMessageAsync(serviceBusMessage); for easier to read code.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda