天蓝色在ps中的色值_天蓝色事件网格集成测试

天蓝色在ps中的色值

Azure Event Grid is a server-less message router. It provides a place for event publishers to send messages to (a topic), and pushes events to interested subscribers (via subscriptions). The question addressed here is: How do we write integration tests for this pub-sub mechanism?

Azure Event Grid是无服务器消息路由器。 它为事件发布者提供了向(主题)发送消息的场所,并(通过订阅)将事件推送给感兴趣的订阅者。 此处解决的问题是:我们如何为此pub-sub机制编写集成测试?

我们正在测试什么? (What are we testing?)

Integration tests should provide you with confidence your setup is working, preferably run on each change as part of a CI/CD pipeline.

集成测试应该使您充满信心,设置可以正常运行,最好在CI / CD管道的每个更改中运行。

To me, a valuable test would be to ensure we can send an event to Event Grid, and then observe it coming out the other side, to be processed by a subscriber. There are few reasons you may want to do this:

对我而言,一项有价值的测试将是确保我们可以将事件发送到事件网格,然后观察它从另一端发出并由订户处理。 您可能有几个理由要这样做:

  • You want to check that you are setting up your infrastructure correctly

    您要检查是否正确设置了基础架构
  • You can verify the integrity of the event on the subscriber side

    您可以在订户端验证事件的完整性

问题(The problem)

Posting an event to a topic boils down to making a HTTP request to the endpoint exposed by the topic. I think this is a solved problem so I won’t cover the details here.

将事件发布到主题归结为对主题公开的端点发出HTTP请求。 我认为这是一个已解决的问题,因此在此不再赘述。

However, when setting up a subscription to a topic you must provide a webhook endpoint. In production, this is going to be a genuine API that you can’t really just spam when executing integration tests. Of course you could set up your own API and have it execute your test code but that’s a faff, right?

但是,设置主题订阅时,必须提供一个Webhook端点。 在生产中,这将是一个真正的API,您在执行集成测试时不能真的只是垃圾邮件。 当然,您可以设置自己的API并让其执行您的测试代码,但这很麻烦,对吗?

Wouldn’t it be great if we could just set up a subscription and then just “listen” to events being delivered to it? This way we wouldn’t have to set up a genuine API — we would have access to the events by opening up some sort of channel. This where Azure Relay Hybrid Connections provide welcome help.

如果我们可以先设置一个订阅然后仅“监听”传递给它的事件,那不是很好吗? 这样,我们就不必建立一个真正的API -我们可以通过打开某种渠道来访问事件。 Azure Relay混合连接在此提供欢迎的帮助。

解决方案的关键 (The key to the solution)

Azure Relay allows you to expose a public endpoint in your application without having to open up any ports on your side. Clients can simply communicate with Azure Relay which will relay, hence the name, all requests to your application.

Azure Relay允许您在应用程序中公开公共终结点,而无需在自己的侧面打开任何端口。 客户端可以简单地与Azure中继进行通信,该中继将中继所有对您应用程序的请求,因此名称也是如此。

实作 (Implementation)

Before writing code, we need to actually create the required Azure resources. I’d recommend creating specific resources just for testing purposes. Might seem like a faff that your tests might have to setup up infrastructure just to execute a test but this is no different to having to setup a database just for integration tests, for example.

在编写代码之前,我们需要实际创建所需的Azure资源。 我建议创建仅用于测试目的的特定资源。 您的测试可能似乎很麻烦,您的测试可能必须仅设置基础结构才能执行测试,但这与仅针对集成测试而设置数据库没有什么不同。

建立 (Setup)

We will need:

我们会需要:

  • A Relay, with a Hybrid Connection — see the docs for creating one. It is simple enough via the Portal UI.

    具有混合连接的中继-请参阅有关创建中继的文档。 通过门户网站UI足够简单。

  • A Topic to send events to — again, can be done easily enough in the Portal UI. You will need to specify the event schema, which will dictate the shape of the events you send/receive.

    同样,可以在Portal UI中轻松完成将事件发送到的主题。 您将需要指定事件架构,该架构将决定您发送/接收的事件的形状。

  • A Subscription, on that topic, that uses the above Hybrid Connection URL as the endpoint. Via the Portal UI navigate to the Topic you just created, add a subscription. You will get the option when specifying the endpoint to select our Hybrid Connection.

    关于该主题的订阅,使用上面的混合连接URL作为端点。 通过门户网站UI导航到您刚刚创建的主题,添加订阅。 当指定端点以选择我们的混合连接时,您将获得该选项。

This can all be done manually in the UI, or can be automated via something like terraform or the Azure CLI.

这些都可以在UI中手动完成,也可以通过terraform或Azure CLI等自动化。

编写测试 (Writing the test)

If you are using C#, Microsoft provides a client that allows you to open up a connection to the Relay. Once this channel is opened up, it will receive any traffic that comes through (i.e. our events!), which we can then execute some code against. This is really the final piece, giving us the entry point into receiving events and asserting against them, without the overhead of creating a separate API.

如果您使用的是C#,Microsoft提供了一个客户端,您可以通过该客户端打开与中继的连接。 打开此通道后,它将接收通过的所有流量(即,我们的事件!),然后我们可以针对该流量执行一些代码。 这确实是最后一步,它为我们提供了接收事件并对其进行断言的切入点,而无需创建单独的API。

Roughly, the code could do something like this:

大致而言,代码可以执行以下操作:

  • Open a connection to the Relay, and start listening

    打开与中继的连接,然后开始收听

  • Post event to the Topic

    将活动发布到主题
  • Use the connection to observe the incoming event(s), and execute any assertions.

    使用该连接来观察传入的事件,并执行所有断言。

Posting to the topic involves making a HTTP request to the Event Grid Topic endpoint. If you are using C#, you can achieve this easily using the HttpClient class.

发布到主题涉及向事件网格主题端点发出HTTP请求。 如果使用的是C#,则可以使用HttpClient类轻松实现此目的。

For more information on how to interact with the Microsoft.Azure.Relay package, see this article.

有关如何与Microsoft.Azure.Relay包进行交互的更多信息,请参见本文

翻译自: https://medium.com/@mikerogers1357/azure-event-grid-integration-testing-b59eae664661

天蓝色在ps中的色值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值