How can I unit test the AppEngine Channel Service?

2019-07-04 08:39发布

I have read the AppEngine unit testing guidelines, and I got the Java Datastore service tests working, but I'm not having any luck with the Channel service. The guide doesn't give any specific examples for channel testing, and the javadocs aren't of much help either, but my IDE is showing me some classes that seem to be meant for unit testing a local channel service; I just can't figure out how to use them.

Does anyone have any experience or examples testing the GAE Channel Service?

3条回答
看我几分像从前
2楼-- · 2019-07-04 09:10
唯我独甜
3楼-- · 2019-07-04 09:12

Ivan provided neat Python example, here's a solution for Java.

Supppose here's the code we want to test

public class ClientChannelService {

    private ChannelService channelService = ChannelServiceFactory.getChannelService();

    public String createToken(){
        return channelService.createChannel(UUID.randomUUID().toString());
    }

    public void sendMessage(String token, String message){
        channelService.sendMessage(new ChannelMessage(token, message));
    }
}

First, add appengine-testing.jar to the classpath:

<dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-testing</artifactId>
   <version>${appengine.version}</version>
   <scope>test</scope>
</dependency>

Next, create a test as follows. I'm assuming JUnit here, but generaly you are free to use any test framework, it doesn't matter for GAE.

private ClientChannelService service;
private LocalServiceTestHelper helper = new LocalServiceTestHelper(
                                            new LocalChannelServiceTestConfig());
private ChannelManager channelManager;

@Before
public void setUp() {
    helper.setUp();
    channelManager = LocalChannelServiceTestConfig.getLocalChannelService()
                                                  .getChannelManager();
    service = new ClientChannelService();
}

@After
public void tearDown() {
    helper.tearDown();
}

@Test
public void testSendMessage() {
    String token = service.createToken();
    connectionId = channelManager.connectClient(token); //emulate client connection

    service.sendMessage(token, "message");

    String message = channelManager.getNextClientMessage(token, connectionId);
    assertEquals("message", message);
}
查看更多
Anthone
4楼-- · 2019-07-04 09:15

The following has worked for me:

import unittest

from google.appengine.api import channel
from google.appengine.ext import testbed


class TestCase(unittest.TestCase):

  def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_channel_stub()

  def test_send(self):
    channel_stub = self.testbed.get_stub('channel')
    token = channel.create_channel('ClientID1')
    channel_stub.connect_channel(token)
    channel.send_message('ClientID1', 'hello')
    channel_messages = channel_stub.get_channel_messages(token)
    channel_stub.clear_channel_messages(token)
    self.assertEquals(['hello'], channel_messages)


if __name__ == '__main__':
  unittest.main()

You can also look at the source code for the channel service stub.

查看更多
登录 后发表回答