1 /**********************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Matthew Khouzam - Initial implementation
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.lttng2
.control
.core
.tests
.relayd
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertTrue
;
19 import java
.io
.DataInputStream
;
20 import java
.io
.DataOutputStream
;
21 import java
.io
.IOException
;
22 import java
.net
.InetAddress
;
23 import java
.net
.Socket
;
24 import java
.util
.List
;
26 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.ILttngRelaydConnector
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.LttngRelaydConnectorFactory
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.AttachSessionResponse
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.Command
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.ConnectResponse
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.CreateSessionResponse
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.CreateSessionReturnCode
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.GetNextIndex
;
34 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.IndexResponse
;
35 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.NextIndexReturnCode
;
36 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.SessionResponse
;
37 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.StreamResponse
;
38 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.core
.relayd
.lttngviewerCommands
.ViewerCommand
;
39 import org
.junit
.Test
;
42 * Unit tests for lttng-relayd. It actually allows us to test the API.
44 * @author Matthew Khouzam
46 public class LttngRelayd24Test
{
48 private static final int PACKETS_TO_READ
= 100;
49 private static final String ADDRESS
= "127.0.0.1"; // change me //$NON-NLS-1$
50 private static final int PORT
= 5344;
52 private static void getPackets(AttachSessionResponse attachedSession
, Socket connection
, ILttngRelaydConnector relayD
) throws IOException
{
53 int numPacketsReceived
= 0;
54 DataOutputStream fOutNet
= new DataOutputStream(connection
.getOutputStream());
55 DataInputStream fInNet
= new DataInputStream(connection
.getInputStream());
56 while (numPacketsReceived
< PACKETS_TO_READ
) {
57 for (StreamResponse stream
: attachedSession
.getStreamList()) {
58 if (stream
.getMetadataFlag() != 1) {
59 ConnectResponse connectPayload
= new ConnectResponse(fInNet
);
60 assertNotNull(connectPayload
);
62 ViewerCommand connectCommand
= new ViewerCommand(Command
.VIEWER_GET_NEXT_INDEX
, ConnectResponse
.SIZE
, 0);
63 fOutNet
.write(connectCommand
.serialize());
66 GetNextIndex indexRequest
= new GetNextIndex(stream
.getId());
67 fOutNet
.write(indexRequest
.serialize());
70 IndexResponse indexReply
= new IndexResponse(fInNet
);
71 // Nothing else supported for now
72 if (indexReply
.getStatus() == NextIndexReturnCode
.VIEWER_INDEX_OK
) {
73 if (relayD
.getPacketFromStream(indexReply
, stream
.getId()) != null) {
89 public void testViewerConnection() throws IOException
{
90 InetAddress addr
= InetAddress
.getByName(ADDRESS
);
91 try (Socket connection
= new Socket(addr
, PORT
);
92 ILttngRelaydConnector relayD
= LttngRelaydConnectorFactory
.getNewConnector(connection
);) {
94 List
<SessionResponse
> sessions
= relayD
.getSessions();
95 assertTrue(sessions
.size() > 0);
96 SessionResponse lttngViewerSession
= sessions
.get(0);
97 assertNotNull(lttngViewerSession
);
98 CreateSessionResponse createSession
= relayD
.createSession();
99 assertEquals(createSession
.getStatus(), CreateSessionReturnCode
.LTTNG_VIEWER_CREATE_SESSION_OK
);
100 AttachSessionResponse attachedSession
= relayD
.attachToSession(lttngViewerSession
);
102 String metaData
= relayD
.getMetadata(attachedSession
);
103 assertNotNull(metaData
);
105 getPackets(attachedSession
, connection
, relayD
);