xref: /unit/test/unit/applications/proto.py (revision 1028)
11019Szelenkov@nginx.comimport re
21019Szelenkov@nginx.comimport time
31019Szelenkov@nginx.comfrom unit.control import TestControl
41019Szelenkov@nginx.com
51019Szelenkov@nginx.com
61019Szelenkov@nginx.comclass TestApplicationProto(TestControl):
71019Szelenkov@nginx.com    def sec_epoch(self):
81019Szelenkov@nginx.com        return time.mktime(time.gmtime())
91019Szelenkov@nginx.com
101019Szelenkov@nginx.com    def date_to_sec_epoch(self, date, template='%a, %d %b %Y %H:%M:%S %Z'):
111019Szelenkov@nginx.com        return time.mktime(time.strptime(date, template))
121019Szelenkov@nginx.com
131027Szelenkov@nginx.com    def search_in_log(self, pattern, name='unit.log'):
141027Szelenkov@nginx.com        with open(self.testdir + '/' + name, 'r', errors='ignore') as f:
151019Szelenkov@nginx.com            return re.search(pattern, f.read())
16*1028Szelenkov@nginx.com
17*1028Szelenkov@nginx.com    def wait_for_record(self, pattern, name='unit.log'):
18*1028Szelenkov@nginx.com        for i in range(50):
19*1028Szelenkov@nginx.com            found = self.search_in_log(pattern, name)
20*1028Szelenkov@nginx.com
21*1028Szelenkov@nginx.com            if found is not None:
22*1028Szelenkov@nginx.com                break
23*1028Szelenkov@nginx.com
24*1028Szelenkov@nginx.com            time.sleep(0.1)
25*1028Szelenkov@nginx.com
26*1028Szelenkov@nginx.com        return found
27