3131import org .apache .htrace .SpanReceiver ;
3232import org .apache .htrace .Trace ;
3333import org .apache .htrace .TraceScope ;
34- import org .junit .AfterClass ;
34+ import org .junit .After ;
3535import org .junit .Assert ;
3636import org .junit .Before ;
3737import org .junit .BeforeClass ;
@@ -56,27 +56,26 @@ public class TestTracing {
5656 private static SpanReceiverHost spanReceiverHost ;
5757
5858 @ Test
59- public void testGetSpanReceiverHost () throws Exception {
60- Configuration c = new Configuration ();
59+ public void testTracing () throws Exception {
6160 // getting instance already loaded.
62- c .set (SpanReceiverHost .SPAN_RECEIVERS_CONF_KEY , "" );
63- SpanReceiverHost s = SpanReceiverHost .getInstance (c );
64- Assert .assertEquals (spanReceiverHost , s );
61+ Assert .assertEquals (spanReceiverHost ,
62+ SpanReceiverHost .getInstance (new Configuration ()));
63+
64+ // write and read without tracing started
65+ String fileName = "testTracingDisabled.dat" ;
66+ writeTestFile (fileName );
67+ Assert .assertTrue (SetSpanReceiver .SetHolder .size () == 0 );
68+ readTestFile (fileName );
69+ Assert .assertTrue (SetSpanReceiver .SetHolder .size () == 0 );
70+
71+ writeWithTracing ();
72+ readWithTracing ();
6573 }
6674
67- @ Test
68- public void testWriteTraceHooks () throws Exception {
75+ public void writeWithTracing () throws Exception {
6976 long startTime = System .currentTimeMillis ();
7077 TraceScope ts = Trace .startSpan ("testWriteTraceHooks" , Sampler .ALWAYS );
71- Path file = new Path ("traceWriteTest.dat" );
72- FSDataOutputStream stream = dfs .create (file );
73-
74- for (int i = 0 ; i < 10 ; i ++) {
75- byte [] data = RandomStringUtils .randomAlphabetic (102400 ).getBytes ();
76- stream .write (data );
77- }
78- stream .hflush ();
79- stream .close ();
78+ writeTestFile ("testWriteTraceHooks.dat" );
8079 long endTime = System .currentTimeMillis ();
8180 ts .close ();
8281
@@ -125,55 +124,17 @@ public void testWriteTraceHooks() throws Exception {
125124 Assert .assertEquals (ts .getSpan ().getTraceId (), span .getTraceId ());
126125 }
127126 }
127+ SetSpanReceiver .SetHolder .spans .clear ();
128128 }
129129
130- @ Test
131- public void testWriteWithoutTraceHooks () throws Exception {
132- Path file = new Path ("withoutTraceWriteTest.dat" );
133- FSDataOutputStream stream = dfs .create (file );
134- for (int i = 0 ; i < 10 ; i ++) {
135- byte [] data = RandomStringUtils .randomAlphabetic (102400 ).getBytes ();
136- stream .write (data );
137- }
138- stream .hflush ();
139- stream .close ();
140- Assert .assertTrue (SetSpanReceiver .SetHolder .size () == 0 );
141- }
142-
143- @ Test
144- public void testReadTraceHooks () throws Exception {
145- String fileName = "traceReadTest.dat" ;
146- Path filePath = new Path (fileName );
147-
148- // Create the file.
149- FSDataOutputStream ostream = dfs .create (filePath );
150- for (int i = 0 ; i < 50 ; i ++) {
151- byte [] data = RandomStringUtils .randomAlphabetic (10240 ).getBytes ();
152- ostream .write (data );
153- }
154- ostream .close ();
155-
156-
130+ public void readWithTracing () throws Exception {
131+ String fileName = "testReadTraceHooks.dat" ;
132+ writeTestFile (fileName );
157133 long startTime = System .currentTimeMillis ();
158134 TraceScope ts = Trace .startSpan ("testReadTraceHooks" , Sampler .ALWAYS );
159- FSDataInputStream istream = dfs .open (filePath , 10240 );
160- ByteBuffer buf = ByteBuffer .allocate (10240 );
161-
162- int count = 0 ;
163- try {
164- while (istream .read (buf ) > 0 ) {
165- count += 1 ;
166- buf .clear ();
167- istream .seek (istream .getPos () + 5 );
168- }
169- } catch (IOException ioe ) {
170- // Ignore this it's probably a seek after eof.
171- } finally {
172- istream .close ();
173- }
174- ts .getSpan ().addTimelineAnnotation ("count: " + count );
175- long endTime = System .currentTimeMillis ();
135+ readTestFile (fileName );
176136 ts .close ();
137+ long endTime = System .currentTimeMillis ();
177138
178139 String [] expectedSpanNames = {
179140 "testReadTraceHooks" ,
@@ -198,21 +159,22 @@ public void testReadTraceHooks() throws Exception {
198159 for (Span span : SetSpanReceiver .SetHolder .spans .values ()) {
199160 Assert .assertEquals (ts .getSpan ().getTraceId (), span .getTraceId ());
200161 }
162+ SetSpanReceiver .SetHolder .spans .clear ();
201163 }
202164
203- @ Test
204- public void testReadWithoutTraceHooks () throws Exception {
205- String fileName = "withoutTraceReadTest.dat" ;
206- Path filePath = new Path (fileName );
207-
208- // Create the file.
209- FSDataOutputStream ostream = dfs .create (filePath );
210- for (int i = 0 ; i < 50 ; i ++) {
211- byte [] data = RandomStringUtils .randomAlphabetic (10240 ).getBytes ();
212- ostream .write (data );
165+ private void writeTestFile (String testFileName ) throws Exception {
166+ Path filePath = new Path (testFileName );
167+ FSDataOutputStream stream = dfs .create (filePath );
168+ for (int i = 0 ; i < 10 ; i ++) {
169+ byte [] data = RandomStringUtils .randomAlphabetic (102400 ).getBytes ();
170+ stream .write (data );
213171 }
214- ostream .close ();
172+ stream .hsync ();
173+ stream .close ();
174+ }
215175
176+ private void readTestFile (String testFileName ) throws Exception {
177+ Path filePath = new Path (testFileName );
216178 FSDataInputStream istream = dfs .open (filePath , 10240 );
217179 ByteBuffer buf = ByteBuffer .allocate (10240 );
218180
@@ -228,32 +190,29 @@ public void testReadWithoutTraceHooks() throws Exception {
228190 } finally {
229191 istream .close ();
230192 }
231- Assert .assertTrue (SetSpanReceiver .SetHolder .size () == 0 );
232- }
233-
234- @ Before
235- public void cleanSet () {
236- SetSpanReceiver .SetHolder .spans .clear ();
237193 }
238194
239195 @ BeforeClass
240- public static void setupCluster () throws IOException {
196+ public static void setup () throws IOException {
241197 conf = new Configuration ();
242198 conf .setLong ("dfs.blocksize" , 100 * 1024 );
243199 conf .set (SpanReceiverHost .SPAN_RECEIVERS_CONF_KEY ,
244200 SetSpanReceiver .class .getName ());
201+ spanReceiverHost = SpanReceiverHost .getInstance (conf );
202+ }
245203
204+ @ Before
205+ public void startCluster () throws IOException {
246206 cluster = new MiniDFSCluster .Builder (conf )
247207 .numDataNodes (3 )
248208 .build ();
249209 cluster .waitActive ();
250-
251210 dfs = cluster .getFileSystem ();
252- spanReceiverHost = SpanReceiverHost . getInstance ( conf );
211+ SetSpanReceiver . SetHolder . spans . clear ( );
253212 }
254213
255- @ AfterClass
256- public static void shutDown () throws IOException {
214+ @ After
215+ public void shutDown () throws IOException {
257216 cluster .shutdown ();
258217 }
259218
0 commit comments