p4lang / ptf

Packet Test Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PTF creates double log entries

karusher opened this issue · comments

I have been using PTF with the default logging settings. This configuration has all testcases write to the same log file (ptf.log).

I have noticed that from the second testcase onwards, all log entries are duplicated. For example:

20:05:24.469  root      : INFO    : ** START TEST CASE session_hello.HelloWhenSendRefreshShouldSucceed
20:05:24.469  root      : INFO    : ** START TEST CASE session_hello.HelloWhenSendRefreshShouldSucceed

After debugging, I determined that src/ptf/init.py - open_logfile is not cleaning up the handlers from the previous test case properly.

This function iterates through the existing handlers and removes them:

    # Remove any existing handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)
        handler.close()

Unfortunately, Python does not support mutating a list while iterating through it with a for loop. The result was that some of the handlers were not deleted.

Instead, we need to create a copy of the list before modifying. The easiest way is with a slice as described by:

https://stackoverflow.com/questions/41443336/python-2-7-remove-handler-object-or-logger

The code would then become:

    # Remove any existing handlers
    for handler in logger.handlers[:]:
        logger.removeHandler(handler)
        handler.close()

I am using Python 2.7 in my development environment.

I think it is the same issue as #104

I think you are right.