Exemplo n.º 1
0
def test_post_samples(mock_post):
  # Create a mocked response object.
  mock_r = mock.create_autospec(requests.Response)
  mock_r.json = mock.MagicMock(return_value=dict(stream_id='test_stream'))
  mock_post.return_value = mock_r

  # Do the actual call.
  samp = Samples(np.random.randn(2, 6), 123.4)
  stream_id = idport.post_samples(
    'http://example.com', 'test-user', 'test-stream', samp)

  # Verify URL and data posted to URL.
  (url,), kwargs = mock_post.call_args
  assert url == 'http://example.com/u/test-user/s/test-stream/samples'
  assert kwargs['data'] == samp.tostring()

  # Check that errors are raised.
  mock_r.raise_for_status.assert_called_with()
Exemplo n.º 2
0
  i = H.nSamples - 1
  log.info('Starting streaming from sample %d...', i)

  while True:
    nsamples, nevents = ftc.poll()
    log.debug('Server holds %d samples and %d events.', nsamples, nevents)
    assert nevents == 0, 'Handling of events is not implemented!'
    lag = nsamples - i
    if lag > (stream_config.sample_rate * .1):
      log.warning('Lagging %d samples', lag)
    assert lag < stream_config.sample_rate * .2

    log.debug('Waiting for new data...')
    ftc.wait(i + args.chunk_size, 0, 1000)  # TODO: check for off-by-one errors.

    # Get new samples and verify it's dimensions.
    D = ftc.getData([i, i + args.chunk_size - 1])
    log.debug(D)
    assert D != None, 'Timeout while reading samples!'
    assert D.shape[0] == args.chunk_size
    i += D.shape[0]

    # Post samples to server.
    idport.post_samples(args.idport_url, args.user_id, stream_id, 
      idport.Samples(D, time.time()))
        
    time.sleep(.01)
  
  log.info('Disconnecting from FieldTrip buffer...')
  ftc.disconnect()