Testing asynchronous polling service in RxJava - unit-testing

I implemented a service that polls for eyetracking events and pushes the received data using a FlowableProcessor (PublishSubject in RxJava 1.x). The whole polling runs in a different thread that can be started and stopped using the services class methods.
I wrote a test for the service, but i am not quite sure if I am doing the right thing.
My idea:
Subscribe to the stream provided by the stream service
Start the service
Wait for the onComplete event (published automatically when the service is supplied with a non-infinite data source mock)
Verify the results
This is my testing code:
StreamService service = new StreamService(mockSource);
Flowable<ETData> stream = service.getStream();
TestSubscriber<ETData> testSub = new TestSubscriber<>();
stream.subscribe(testSub);
service.start();
testSub.await();
testSub.assertNoErrors();
testSub.assertComplete();
List<ETData> received = testSub.values();
assertEquals(2, received.size());
assertEquals(mockData0, received.get(0));
assertEquals(mockData1, received.get(1));
Is this okay, or am i missing something?

Related

Grpc server with async and sync services at the same time

I need to be able to serve responses for some particular requests from the main thread, while the rest can arrive from any thread. With that in mind,
I created a GRPC server which has 2 services, one is implemented as an AsyncService, and the other as a sync service.
However, when adding a completion queue, the sync service no longer responds to requests.
builder.RegisterService(this); // this inherits from Service (sync)
builder.RegisterService(&m_service); // m_services is an AsyncService
m_mainThreadQueue = builder.AddCompletionQueue();
m_server = std::unique_ptr<Server>(builder.BuildAndStart());
{
(new GrabSnapshotCallData(this, &m_service, m_mainThreadQueue.get()))->Proceed();
}
m_server->Wait();
Adding the completion queue makes the sync service no longer response to requests.
I couldn't find much information about this particular topic anywhere, so perhaps it is not really supported in grpc.
So, is there a way to have both async and sync services simultaneously on the same server? If not, what should I do to emulate that behavior

EventHubProducerClient for publishing events regularly

My application creates a new EventHubClientBuilder() every time it sends an event to EventHub.
EventHubProducerClient producer = new EventHubClientBuilder()
.connectionString(connectionString, eventHubName)
.buildProducerClient();
I followed this quick start guide when building this application. I don't think it is best practice to create a new client if the application is expected to be publishing an event regularly (every 2-3 min) however, I cannot find any documentation that explains how to keep the client connection open aside from this .NET guide but my application is using Java.
Can someone explain how I could use a single producer client for the duration of the application? The application is consuming messages from another application and needs to published each message to EventHub.

How to subscribe AWS Lambda to Salesforce Platform Events

We want to integrate Salesforce into out Micro Service Structure in AWS.
There is a article about this here
So we want to subscribe lambda to certain platform events in salesforce.
But i found no code examples for this. I gave it a try using node.js (without lambda). This works great:
var jsforce = require('jsforce');
var username = 'xxxxxxxx';
var password = 'xxxxxxxxxxx';
var conn = new jsforce.Connection({loginUrl : 'https://test.salesforce.com'});
conn.login(username, password, function(err, userInfo) {
if (err) { return console.error(err); }
console.error('Connected '+userInfo);
conn.streaming.topic("/event/Contact_Change__e").subscribe(function(message) {
console.dir(message);
});
});
But i am not sure if this is the right way to do it in lambda.
My understanding of Salesforce Platform Events is that they use CometD under the hood. CometD allows the HTTP client (your code) to subscribe to events published by the HTTP server.
This means your client code needs to be running and be in a state where it is subscribed and listening for server events for the duration of time that you expect to be receiving events. In most cases, this duration is indefinate i.e. your client code expects to wait forever in a subscribed state, ready to receive events.
This is at odds with AWS Lambda functions, which are expected to complete execution in a relatively short amount of time (max 15 minutes last time I checked).
I would suggest you need a long running process, such as a nodejs application running in Elastic Beanstalk, or in a container. The nodejs application can stay running indefinately, in a subscribed state. Each time it receives an event, it could call your AWS Lambda function in order to implement the required actions.

synchronous activemq webservice

I have a webservice (Restful) that send a message through ActiveMQ, and synchronously receive the response by creating a temporary listener in the same request.
The problem is, the listener wait for response of synchronous process , but never die. I need that listener receive response, and immediately stop the listener once is responded the request of webservice.
I have a great problem, because for each request of web services, a listener is created and this is active, producing overhead.
That code in the link is not production grade - simply an example how to make a "hello world" request reply.
Here is some psuedo code to deal with consuming responses blocking - and closing the consumer afterwards.
MessageConsumer responseConsumer = session.createConsumer(tempDest);
Messages response = responseConsumer.receive(waitTimeout);
// TODO handle msg
responseConsumer.close();
Temp destinations in JMS are pretty slow anyways. You can instead use JMSCorrelationID and make the replies go to a "regular queue" handled by a single consumer for all replies. That way, you need some thread handling code to hand over the message to the web service thread, but it will be non blocking and very fast.

Glassfish - JMS Request/Response - message doesn't go on queue

I'm trying to implement a web service in Glassfish 3.1.2, using the included OpenMQ JMS queue, that implements a synchronous JMS Request-Response using Temporary queuing for the response. It sends a message that is picked up off the main queue by a remote client job (runs outside of container), and receives back a response on the temporary queue.
In a basic Java POC, this works. But once I put the server-side code into the container, it doesn't work.
I turned off the job so that the messages would just go to the queue and not be picked up, and I follow the queue with QBrowser.
If I simply send the message from the producer, it gets onto the queue and could be read by the job.
But once I add in the code to receive() the response, the message is not readable on the queue. QBrowser says that there is 1 message on the queue, but it is marked UnAck and the queue appears empty (e.g. message is not readable).
connectionFactory and requestQueue are injected as #Resource from glassfish. Main queue is defined in glassfish.
Web Service innards:
connection = connectionFactory .createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(requestQueue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
MyObject myObj=new MyObject();
Message message=session.createObjectMessage(myObj);
TemporaryQueue responseQueue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(responseQueue);
message.setJMSReplyTo(responseQueue);
producer.send(message);
//if I comment out the next line, the message appears on the queue. If I leave it in, it will behave as described above.
Message response=consumer.receive();
I've tried various approaches, including separate connections and sessions and asynchronous consumer, and attempted a Transacted session for the producer but only got stacktraces when trying to commit.
What am I missing to make this get to the queue properly?
Thanks in advance!
Edit: Domain.xml references for ConnectionFactory and Queue:
<connector-connection-pool description="Connection factory for job processing" name="jms/MyJobs"
resource-adapter-name="jmsra" connection-definition-name="javax.jms.ConnectionFactory"
transaction-support=""></connector-connection-pool>
<connector-resource pool-name="jms/MyJobs" jndi-name="jms/MyJobs"></connector-resource>
<admin-object-resource res-adapter="jmsra" res-type="javax.jms.Queue"
description="Queue to request a job process" jndi-name="jms/MyJobRequest">
<property name="Name" value="MyJobRequest"></property>
</admin-object-resource>
[...]
<resource-ref ref="jms/MyJobs"></resource-ref>
<resource-ref ref="jms/MyJobRequest"></resource-ref>
Turned out to be a Transactional issue.
Got around it by adding a new method:
#Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
private void sendMessage(MessageProducer producer, Message message) throws Exception{
producer.send(message);
}