#!perl
use Cassandane::Tiny;

sub test_cmdtimer_sessionid
    :min_version_3_5 :NoStartInstances
    ($self)
{
    # log the timing for anything that takes longer than zero seconds
    $self->{instance}->{config}->set('commandmintimer', '0');
    $self->_start_instances();

    my $imaptalk = $self->{store}->get_client();

    # put a bunch of messages in inbox to make sure fetch isn't instantaneous
    my %msgs;
    foreach my $n (1..5) {
        $msgs{$n} = $self->make_message("message $n");
    }

    $imaptalk->select("INBOX");
    $self->assert_str_equals("ok", $imaptalk->get_last_completion_response());

    # discard buffered syslog output from setup
    $self->{instance}->getsyslog();

    # fetch some things that will take a little while
    $imaptalk->fetch('1:*', '(uid flags body[])');
    $self->assert_str_equals("ok", $imaptalk->get_last_completion_response());

    # should have logged some timer output, which should include the sess id
    if ($self->{instance}->{have_syslog_replacement}) {
        # make sure that the connection is ended so that imapd reset happens
        $imaptalk->logout();
        undef $imaptalk;

        my @lines = $self->{instance}->getsyslog();

        my @timer_lines = grep { m/\bcmdtimer:/ } @lines;
        $self->assert_num_gte(1, scalar @timer_lines);
        foreach my $line (@timer_lines) {
            $self->assert_matches(qr/sessionid=<[^ >]+>/, $line);
        }

        my (@behavior_lines) = grep { /session ended/ } @lines;

        $self->assert_num_gte(1, scalar @behavior_lines);
    }
}
