Commit | Line | Data |
---|---|---|
8e15b929 MK |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial implementation | |
11 | **********************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng2.control.core.tests.relayd; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertNotNull; | |
17 | import static org.junit.Assert.assertTrue; | |
18 | ||
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; | |
25 | ||
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; | |
40 | ||
41 | /** | |
42 | * Unit tests for lttng-relayd. It actually allows us to test the API. | |
43 | * | |
44 | * @author Matthew Khouzam | |
45 | */ | |
46 | public class LttngRelayd24Test { | |
47 | ||
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; | |
51 | ||
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); | |
61 | ||
62 | ViewerCommand connectCommand = new ViewerCommand(Command.VIEWER_GET_NEXT_INDEX, ConnectResponse.SIZE, 0); | |
63 | fOutNet.write(connectCommand.serialize()); | |
64 | fOutNet.flush(); | |
65 | ||
66 | GetNextIndex indexRequest = new GetNextIndex(stream.getId()); | |
67 | fOutNet.write(indexRequest.serialize()); | |
68 | fOutNet.flush(); | |
69 | ||
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) { | |
74 | numPacketsReceived++; | |
75 | } | |
76 | } | |
77 | } | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | /** | |
83 | * Test a connection | |
84 | * | |
85 | * @throws IOException | |
86 | * network timeout? | |
87 | */ | |
88 | @Test | |
89 | public void testViewerConnection() throws IOException { | |
90 | InetAddress addr = InetAddress.getByName(ADDRESS); | |
7b3eb8c0 AM |
91 | try (Socket connection = new Socket(addr, PORT); |
92 | ILttngRelaydConnector relayD = LttngRelaydConnectorFactory.getNewConnector(connection);) { | |
93 | ||
8e15b929 MK |
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); | |
101 | ||
102 | String metaData = relayD.getMetadata(attachedSession); | |
103 | assertNotNull(metaData); | |
104 | ||
105 | getPackets(attachedSession, connection, relayD); | |
106 | } | |
107 | } | |
108 | } |