2525import com .google .cloud .logging .Logging .TailOption ;
2626import com .google .common .collect .ImmutableList ;
2727import java .util .ArrayList ;
28+ import java .util .concurrent .atomic .AtomicBoolean ;
2829import org .junit .AfterClass ;
29- import org .junit .Ignore ;
3030import org .junit .Test ;
3131
3232public class ITTailLogsTest extends BaseSystemTest {
@@ -40,8 +40,7 @@ public static void cleanUpLogs() throws InterruptedException {
4040 assertTrue (cleanupLog (LOG_ID ));
4141 }
4242
43- @ Ignore
44- @ Test (timeout = 120_000 ) // Note: the test should not take longer than 2 min
43+ @ Test (timeout = 30_000 ) // Note: resilient periodic write should succeed in < 30 sec
4544 public void testTailLogEntries () throws InterruptedException {
4645 LogEntry testLogEntry =
4746 LogEntry .newBuilder (Payload .StringPayload .of ("stringPayload1" ))
@@ -54,26 +53,36 @@ public void testTailLogEntries() throws InterruptedException {
5453 String filter = "logName=projects/" + logging .getOptions ().getProjectId () + "/logs/" + LOG_ID ;
5554 LogEntryServerStream stream = logging .tailLogEntries (TailOption .filter (filter ));
5655
56+ AtomicBoolean isFinished = new AtomicBoolean (false );
5757 Runnable task =
5858 () -> {
5959 // it may take awhile for tailing session to startup on the backend
60- // wait 10 sec before sending log entries
61- try {
62- Thread .sleep (10000 );
63- } catch (InterruptedException t ) {
60+ // send log entries periodically until stream intercepts them or test completes
61+ while (!isFinished .get ()) {
62+ try {
63+ Thread .sleep (1000 );
64+ } catch (InterruptedException t ) {
65+ break ;
66+ }
67+ if (!isFinished .get ()) {
68+ logging .write (ImmutableList .of (testLogEntry ));
69+ }
6470 }
65- logging .write (ImmutableList .of (testLogEntry ));
6671 };
6772 Thread thread = new Thread (task );
6873 thread .start ();
6974
7075 final ArrayList <LogEntry > receivedEntries = new ArrayList <>();
7176 for (LogEntry log : stream ) {
72- receivedEntries .add (log );
73- if (receivedEntries .size () > 0 ) {
77+ // Use endsWith() because getLogName() may return a fully qualified path
78+ // (projects/{project_id}/logs/{LOG_ID}) rather than the simple identifier.
79+ if (log .getLogName ().endsWith (LOG_ID )) {
80+ receivedEntries .add (log );
7481 break ;
7582 }
7683 }
84+ isFinished .set (true );
85+ thread .interrupt ();
7786 stream .cancel ();
7887
7988 LogEntry resultEntry = receivedEntries .get (0 );
0 commit comments