View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io.input;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.IOException;
27  import java.time.Duration;
28  import java.time.Instant;
29  
30  import org.apache.commons.io.IOUtils;
31  import org.apache.commons.io.ThreadUtils;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link TimestampedObserver}.
36   */
37  public class TimestampedObserverTest {
38  
39      @Test
40      public void test() throws IOException, InterruptedException {
41          final Instant before = Instant.now();
42          // Some OS' clock granularity may be high.
43          ThreadUtils.sleep(Duration.ofMillis(20));
44          final TimestampedObserver timestampedObserver = new TimestampedObserver();
45          assertFalse(timestampedObserver.isClosed());
46          // Java 8 instant resolution is not great.
47          ThreadUtils.sleep(Duration.ofMillis(20));
48          // toString() should not blow up before close().
49          assertNotNull(timestampedObserver.toString());
50          assertTrue(timestampedObserver.getOpenInstant().isAfter(before));
51          assertTrue(timestampedObserver.getOpenToNowDuration().toNanos() > 0);
52          assertNull(timestampedObserver.getCloseInstant());
53          assertFalse(timestampedObserver.isClosed());
54          final byte[] buffer = MessageDigestInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
55          try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer), timestampedObserver)) {
56              assertTrue(timestampedObserver.getOpenInstant().isAfter(before));
57              assertTrue(timestampedObserver.getOpenToNowDuration().toNanos() > 0);
58              assertFalse(timestampedObserver.isClosed());
59          }
60          assertTrue(timestampedObserver.isClosed());
61          assertTrue(timestampedObserver.getOpenInstant().isAfter(before));
62          assertTrue(timestampedObserver.getOpenToNowDuration().toNanos() > 0);
63          assertTrue(timestampedObserver.getCloseInstant().isAfter(timestampedObserver.getOpenInstant()));
64          assertTrue(timestampedObserver.getOpenToCloseDuration().toNanos() > 0);
65          assertNotNull(timestampedObserver.toString());
66      }
67  
68      @Test
69      public void testExample() throws IOException {
70          final TimestampedObserver timestampedObserver = new TimestampedObserver();
71          final byte[] buffer = MessageDigestInputStreamTest
72              .generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
73          try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer),
74              timestampedObserver)) {
75              //
76          }
77          // System.out.printf("IO duration: %s%n", timestampedObserver);
78          // System.out.printf("IO duration: %s%n", timestampedObserver.getOpenToCloseDuration());
79      }
80  
81  }