1#!/usr/bin/env python3 2# Copyright 2022 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests for the timestamp analyzer.""" 16 17import unittest 18from pw_chrono.timestamp_analyzer import process_snapshot 19from pw_chrono_protos import chrono_pb2 20 21 22class TimestampTest(unittest.TestCase): 23 """Test for the timestamp analyzer.""" 24 25 def test_no_timepoint(self): 26 time_stamps = chrono_pb2.SnapshotTimestamps() 27 self.assertEqual('', str(process_snapshot(time_stamps))) 28 29 def test_timestamp_unknown_epoch_type(self): 30 time_stamps = chrono_pb2.SnapshotTimestamps() 31 32 time_point = chrono_pb2.TimePoint() 33 unkown = chrono_pb2.EpochType.Enum.UNKNOWN 34 time_point.clock_parameters.epoch_type = unkown 35 36 time_stamps.timestamps.append(time_point) 37 38 expected = '\n'.join( 39 ( 40 'Snapshot capture timestamp', 41 ' Time since unknown epoch 0: unknown', 42 ) 43 ) 44 45 self.assertEqual(expected, str(process_snapshot(time_stamps))) 46 47 def test_timestamp_with_time_since_boot(self): 48 time_stamps = chrono_pb2.SnapshotTimestamps() 49 50 time_point = chrono_pb2.TimePoint() 51 time_since_boot = chrono_pb2.EpochType.Enum.TIME_SINCE_BOOT 52 time_point.clock_parameters.epoch_type = time_since_boot 53 time_point.timestamp = 100 54 time_point.clock_parameters.tick_period_seconds_numerator = 1 55 time_point.clock_parameters.tick_period_seconds_denominator = 1000 56 57 time_stamps.timestamps.append(time_point) 58 59 expected = '\n'.join( 60 ('Snapshot capture timestamp', ' Time since boot: 2:24:00') 61 ) 62 63 self.assertEqual(expected, str(process_snapshot(time_stamps))) 64 65 def test_timestamp_with_utc_wall_clock(self): 66 time_stamps = chrono_pb2.SnapshotTimestamps() 67 68 time_point = chrono_pb2.TimePoint() 69 utc_wall_clock = chrono_pb2.EpochType.Enum.UTC_WALL_CLOCK 70 time_point.clock_parameters.epoch_type = utc_wall_clock 71 time_point.timestamp = 100 72 time_point.clock_parameters.tick_period_seconds_numerator = 1 73 time_point.clock_parameters.tick_period_seconds_denominator = 1000 74 75 time_stamps.timestamps.append(time_point) 76 77 expected = '\n'.join( 78 ('Snapshot capture timestamp', ' UTC time: 1970-01-01 02:24:00') 79 ) 80 81 self.assertEqual(expected, str(process_snapshot(time_stamps))) 82 83 def test_timestamp_with_time_since_boot_and_utc_wall_clock(self): 84 time_stamps = chrono_pb2.SnapshotTimestamps() 85 86 time_point = chrono_pb2.TimePoint() 87 time_since_boot = chrono_pb2.EpochType.Enum.TIME_SINCE_BOOT 88 time_point.clock_parameters.epoch_type = time_since_boot 89 time_point.timestamp = 100 90 time_point.clock_parameters.tick_period_seconds_numerator = 1 91 time_point.clock_parameters.tick_period_seconds_denominator = 1000 92 time_stamps.timestamps.append(time_point) 93 94 time_point = chrono_pb2.TimePoint() 95 utc_wall_clock = chrono_pb2.EpochType.Enum.UTC_WALL_CLOCK 96 time_point.clock_parameters.epoch_type = utc_wall_clock 97 time_point.timestamp = 100 98 time_point.clock_parameters.tick_period_seconds_numerator = 1 99 time_point.clock_parameters.tick_period_seconds_denominator = 1000 100 time_stamps.timestamps.append(time_point) 101 102 expected = '\n'.join( 103 ( 104 'Snapshot capture timestamps', 105 ' Time since boot: 2:24:00', 106 ' UTC time: 1970-01-01 02:24:00', 107 ) 108 ) 109 110 self.assertEqual(expected, str(process_snapshot(time_stamps))) 111