You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by "Chris M. Hostetter (Jira)" <ji...@apache.org> on 2021/11/18 00:28:00 UTC

[jira] [Updated] (SOLR-15807) Add a "LogInterceptor" helpe class that makes it easy to inspect & assert expected log messages

     [ https://issues.apache.org/jira/browse/SOLR-15807?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Chris M. Hostetter updated SOLR-15807:
--------------------------------------
    Attachment: SOLR-15807.patch
        Status: Open  (was: Open)

Following along with how {{ErrorLogMuter}} works, this patch refactors away the (hard to use) Log4jListAppender introduced by our sibling Sub-Task for testing {{{}ErrorLogMuter{}}}, and replaces it with an {{AutoClosable}} {{LogInterceptor}} class that can be declared/instantiated with a "fluent" API and provides "Queue" of log messages that have been intercepted.

Some example usage(s)...
{code:java}
// simplest possible usage...
// intercept all erors from the SolrCore logger...
try (LogInterceptor errLog = LogInterceptor.error(SolrCore.class)) {
  // ... do some stuff ...

  // assert that we didn't get any ERROR log messages from the code we just ran...
  assertEquals(0, errLog.getCount());
}

// intercept any WARN level messages under this package that mention "PKI"...
try (LogInterceptor secWarnLog = LogInterceptor.warn("org.apache.solr.security").substring("PKI")) {
  // ... do some stuff ...

  // assert that we got a warning we expect...
  assertThat(secWarnLog.pollMessage(), containsString("hoss")); // convinience method only dealing with method string
  // ... and nothing else...
  assertThat(secWarnLog.getQueue().isEmpty());

  // ... do some more stuff ...

  // assert that we got another warning, and inspect more stuctured data about it...
  var logEvent = secWarnLog.getQueue().poll();
  assertNotNull(logEvent);
  assertEquals("xyz", logEvent.getContextData().getValue("tid")); // check the MDC data of the log message

  // those should be the only warnings we saw from this pacakge matching PKI...
  assertThat(secWarnLog.getQueue().isEmpty());
  assertEquals(2, secWarnLog.getCount())
}

// specify your own queue, so you can "wait" for log messages that you expect to happen due to background threads...
final BlockingQueue<LogEvent> myQueue = new ArrayBlockingQueue<>(100);
try (LogInterceptor commitLogs = LogInterceptor.debug(DirectUpdateHandler2.class).substring("end_commit_flush").setQueue(myQueue)) {

  // ...send an update that has commitWithin ...
  
}
LogEvent event = myQueue.poll(60, TimeUnit.SECONDS); // wait for the autocommit to finish

{code}
----
Patch has lots of nocommits, but they are almost all just related to writing more javadocs.

Curious if folks have concerns/feedback on the API?

> Add a "LogInterceptor" helpe class that makes it easy to inspect & assert expected log messages
> -----------------------------------------------------------------------------------------------
>
>                 Key: SOLR-15807
>                 URL: https://issues.apache.org/jira/browse/SOLR-15807
>             Project: Solr
>          Issue Type: Sub-task
>            Reporter: Chris M. Hostetter
>            Assignee: Chris M. Hostetter
>            Priority: Major
>         Attachments: SOLR-15807.patch
>
>
> Following on some of the ideas started/outlined in the parent task...
> we should have an easy way for a block of test code to "intercept" and make assertions about log messages from specific loggers (at specific log levels, matching specific substrings/regexes) that happen as a result of some actions during the test code.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org