def test_buffering_length(self):
        """
        Verify that we're summing the time a stream spends buffering.
        """
        streams_with_buffer_length = 0
        enum = -1
        for key, events in self.events.items():
            enum += 1
            stats = PBSVideoStats()
            stream_buffer_length = None
            open_start_buffer = False
            is_valid_buffering = True
            media_buffering_start_time = None
            media_buffering_start_location = None
            for event in events:
                stats.add_event(event)
                event_type = event.get('event_type')
                if not event_type:
                    is_valid_buffering = False
                    break
                if event_type == 'MediaBufferingStart':
                    if open_start_buffer:
                        # two MediaBufferingStart events in a row
                        # toss stream
                        is_valid_buffering = False
                        break
                    open_start_buffer = True
                    media_buffering_start_time = parse(event.get('event_date'))
                    media_buffering_start_location = event.get('x_video_location')
                if event_type == 'MediaBufferingEnd':
                    if not open_start_buffer:
                        # two MediaBufferingEnd events in a row
                        # toss stream
                        is_valid_buffering = False
                        break
                    open_start_buffer = False
                    # verify that we didn't scrub video
                    if event.get('x_video_location') != media_buffering_start_location:
                        is_valid_buffering = False
                        break
                    # subtract MediaBufferingEnd timestamp from
                    # MediaBufferingStart timestamp
                    if not stream_buffer_length:
                        stream_buffer_length = 0
                    media_buffering_end_time = parse(event.get('event_date'))
                    buffer_delta = media_buffering_end_time - media_buffering_start_time
                    stream_buffer_length += self._total_seconds(buffer_delta)
            if is_valid_buffering:
                results = stats.summary()
                results_buffering_length = results.get('buffering_length')
                self.assertEqual(results_buffering_length, stream_buffer_length)
                if stream_buffer_length is not None:
                    self.assertTrue(
                        results_buffering_length >= 0,
                        '%s is not a positive number' % results_buffering_length)
                    streams_with_buffer_length += 1
        self.assertTrue(streams_with_buffer_length > 0)
dataset/ETHPy150Open pbs/agora-proc/tests/test_stats.py/PBSVideoStatsTestcase.test_buffering_length/original