예제 #1
0
 def test_basic_recordwriter_write_synced(self):
   test_string = "hello world"
   with self.EphemeralFile('r+') as fp:
     RecordWriter.do_write(fp, test_string, StringCodec(), sync=True)
     fp.seek(0)
     rr = RecordReader(fp)
     assert rr.read() == test_string
예제 #2
0
 def test_basic_recordwriter_write_synced(self):
     test_string = "hello world"
     with self.EphemeralFile('r+') as fp:
         RecordWriter.do_write(fp, test_string, StringCodec(), sync=True)
         fp.seek(0)
         rr = RecordReader(fp)
         assert rr.read() == test_string
예제 #3
0
def test_recordreader_fails_with_writeonly():
    with EphemeralFile('a') as fp:
        with pytest.raises(RecordIO.InvalidFileHandle):
            RecordReader(fp)
    with EphemeralFile('w') as fp:
        with pytest.raises(RecordIO.InvalidFileHandle):
            RecordReader(fp)
예제 #4
0
 def test_basic_recordwriter_write(self):
   test_string = "hello world"
   with self.EphemeralFile('r+') as fp:
     rw = RecordWriter(fp)
     rw.write(test_string)
     fp.seek(0)
     rr = RecordReader(fp)
     assert rr.read() == test_string
예제 #5
0
 def test_basic_recordwriter_write(self):
     test_string = "hello world"
     with self.EphemeralFile('r+') as fp:
         rw = RecordWriter(fp)
         rw.write(test_string)
         fp.seek(0)
         rr = RecordReader(fp)
         assert rr.read() == test_string
예제 #6
0
def test_basic_recordwriter_write_synced():
  test_string = "hello world"
  with EphemeralFile('w') as fp:
    fn = fp.name
    RecordWriter.do_write(fp, test_string, RecordIO.StringCodec(), sync=True)
    with open(fn) as fpr:
      rr = RecordReader(fpr)
      assert rr.read() == test_string
예제 #7
0
 def test_premature_end_of_stream(self):
   with self.EphemeralFile('r+') as fp:
     fpr = FileLike.get(fp)
     fpr = fp
     fpr.write(struct.pack('>L', 1))
     fpr.seek(0)
     rr = RecordReader(fpr)
     with pytest.raises(RecordIO.PrematureEndOfStream):
       rr.read()
예제 #8
0
 def test_premature_end_of_stream(self):
     with self.EphemeralFile('r+') as fp:
         fpr = FileLike.get(fp)
         fpr = fp
         fpr.write(struct.pack('>L', 1))
         fpr.seek(0)
         rr = RecordReader(fpr)
         with pytest.raises(RecordIO.PrematureEndOfStream):
             rr.read()
예제 #9
0
def test_basic_recordwriter_write():
    test_string = "hello world"
    with EphemeralFile('w') as fp:
        fn = fp.name
        rw = RecordWriter(fp)
        rw.write(test_string)
        rw.close()
        with open(fn) as fpr:
            rr = RecordReader(fpr)
            assert rr.read() == test_string
예제 #10
0
def test_basic_recordwriter_write():
  test_string = "hello world"
  with EphemeralFile('w') as fp:
    fn = fp.name
    rw = RecordWriter(fp)
    rw.write(test_string)
    rw.close()
    with open(fn) as fpr:
      rr = RecordReader(fpr)
      assert rr.read() == test_string
예제 #11
0
def test_recordreader_works_with_plus():
    with EphemeralFile('a+') as fp:
        try:
            RecordReader(fp)
        except:
            assert False, 'Failed to initialize RecordWriter in r+ mode'
    with EphemeralFile('w+') as fp:
        try:
            RecordReader(fp)
        except:
            assert False, 'Failed to initialize RecordWriter in r+ mode'
예제 #12
0
    def test_record_too_large(self):
        with self.EphemeralFile('r+') as fp:
            fpw = FileLike.get(fp)
            fpw.write(struct.pack('>L', RecordIO.MAXIMUM_RECORD_SIZE + 1))
            fpw.write('a')
            fpw.flush()
            fpw.seek(0)

            rr = RecordReader(fp)
            with pytest.raises(RecordIO.RecordSizeExceeded):
                rr.read()
예제 #13
0
  def test_record_too_large(self):
    with self.EphemeralFile('r+') as fp:
      fpw = FileLike.get(fp)
      fpw.write(struct.pack('>L', RecordIO.MAXIMUM_RECORD_SIZE+1))
      fpw.write('a')
      fpw.flush()
      fpw.seek(0)

      rr = RecordReader(fp)
      with pytest.raises(RecordIO.RecordSizeExceeded):
        rr.read()
예제 #14
0
def test_premature_end_of_stream():
  with EphemeralFile('w') as fp:
    fn = fp.name

    fp.write(struct.pack('>L', 1))
    fp.close()

    with open(fn) as fpr:
      rr = RecordReader(fpr)
      with pytest.raises(RecordIO.PrematureEndOfStream):
        rr.read()
예제 #15
0
def test_premature_end_of_stream():
    with EphemeralFile('w') as fp:
        fn = fp.name

        fp.write(struct.pack('>L', 1))
        fp.close()

        with open(fn) as fpr:
            rr = RecordReader(fpr)
            with pytest.raises(RecordIO.PrematureEndOfStream):
                rr.read()
예제 #16
0
def test_sanity_check_bytes():
  with EphemeralFile('w') as fp:
    fn = fp.name

    fp.write(struct.pack('>L', RecordIO.SANITY_CHECK_BYTES+1))
    fp.write('a')
    fp.close()

    with open(fn) as fpr:
      rr = RecordReader(fpr)
      with pytest.raises(RecordIO.RecordSizeExceeded):
        rr.read()
예제 #17
0
def test_sanity_check_bytes():
    with EphemeralFile('w') as fp:
        fn = fp.name

        fp.write(struct.pack('>L', RecordIO.SANITY_CHECK_BYTES + 1))
        fp.write('a')
        fp.close()

        with open(fn) as fpr:
            rr = RecordReader(fpr)
            with pytest.raises(RecordIO.RecordSizeExceeded):
                rr.read()
예제 #18
0
  def test_basic_recordwriter_write_synced_raises(self):
    test_string = "hello world"
    self.mox.StubOutWithMock(os, 'fsync')
    with RecordioTestBase.EphemeralFile('r+') as fp:
      os.fsync(fp.fileno()).AndRaise(OSError)

      self.mox.ReplayAll()

      rw = RecordWriter(FileLike(fp))
      rw.set_sync(True)
      rw.write(test_string)
      fp.seek(0)
      rr = RecordReader(fp)
      assert rr.read() == test_string
예제 #19
0
    def test_basic_recordwriter_write_synced_raises(self):
        test_string = "hello world"
        self.mox.StubOutWithMock(os, 'fsync')
        with RecordioTestBase.EphemeralFile('r+') as fp:
            os.fsync(fp.fileno()).AndRaise(OSError)

            self.mox.ReplayAll()

            rw = RecordWriter(FileLike(fp))
            rw.set_sync(True)
            rw.write(test_string)
            fp.seek(0)
            rr = RecordReader(fp)
            assert rr.read() == test_string
예제 #20
0
  def test_bad_header_size(self):
    with self.EphemeralFile('r+') as fp:
      fpw = FileLike.get(fp)
      fpw.write(struct.pack('>L', RecordIO.MAXIMUM_RECORD_SIZE))
      fpw._fp.truncate(RecordIO.RECORD_HEADER_SIZE - 1)
      fpw.flush()
      fpw.seek(0)

      rr = RecordReader(fp)
      with pytest.raises(RecordIO.PrematureEndOfStream):
        rr.read()
      assert fpw.tell() != 0
      fpw.seek(0)
      assert rr.try_read() is None
      assert fpw.tell() == 0
예제 #21
0
    def test_bad_header_size(self):
        with self.EphemeralFile('r+') as fp:
            fpw = FileLike.get(fp)
            fpw.write(struct.pack('>L', RecordIO.MAXIMUM_RECORD_SIZE))
            fpw._fp.truncate(RecordIO.RECORD_HEADER_SIZE - 1)
            fpw.flush()
            fpw.seek(0)

            rr = RecordReader(fp)
            with pytest.raises(RecordIO.PrematureEndOfStream):
                rr.read()
            assert fpw.tell() != 0
            fpw.seek(0)
            assert rr.try_read() is None
            assert fpw.tell() == 0
예제 #22
0
    def test_paranoid_append_framing(self):
        with self.DurableFile('w') as fp:
            fn = fp.name

        test_string_1 = "hello world"
        test_string_2 = "ahoy ahoy, bonjour"

        RecordWriter.append(fn, test_string_1)
        RecordWriter.append(fn, test_string_2)

        with open(fn) as fpr:
            rr = RecordReader(fpr)
            assert rr.read() == test_string_1
            assert rr.read() == test_string_2

        os.remove(fn)
예제 #23
0
  def test_paranoid_append_framing(self):
    with self.DurableFile('w') as fp:
      fn = fp.name

    test_string_1 = "hello world"
    test_string_2 = "ahoy ahoy, bonjour"

    RecordWriter.append(fn, test_string_1)
    RecordWriter.append(fn, test_string_2)

    with open(fn) as fpr:
      rr = RecordReader(fpr)
      assert rr.read() == test_string_1
      assert rr.read() == test_string_2

    os.remove(fn)
예제 #24
0
 def test_basic_recordreader_iterator(self):
     test_strings = ["hello", "world", "etc"]
     with self.EphemeralFile('r+') as fp:
         for string in test_strings:
             RecordWriter.do_write(fp, string, StringCodec(), sync=True)
         fp.seek(0)
         rr = RecordReader(fp)
         assert list(rr) == test_strings
예제 #25
0
 def test_recordreader_works_with_plus(self):
   for mode in ('a+', 'w+'):
     with self.EphemeralFile(mode) as fp:
       try:
         RecordReader(fp)
       except Exception as e:
         assert False, (
             "Failed to initialize RecordReader in '%s' mode (exception: %s)" % (mode, e))
예제 #26
0
  def test_recordwriter_framing(self):
    test_string_1 = "hello world"
    test_string_2 = "ahoy ahoy, bonjour"

    with self.EphemeralFile('w') as fp:
      fn = fp.name
      rw = RecordWriter(fp)
      rw.write(test_string_1)
      rw.close()

      with open(fn, 'a') as fpa:
        rw = RecordWriter(fpa)
        rw.write(test_string_2)

      with open(fn) as fpr:
        rr = RecordReader(fpr)
        assert rr.read() == test_string_1
        assert rr.read() == test_string_2
예제 #27
0
    def test_recordwriter_framing(self):
        test_string_1 = "hello world"
        test_string_2 = "ahoy ahoy, bonjour"

        with self.EphemeralFile('w') as fp:
            fn = fp.name
            rw = RecordWriter(fp)
            rw.write(test_string_1)
            rw.close()

            with open(fn, 'a') as fpa:
                rw = RecordWriter(fpa)
                rw.write(test_string_2)

            with open(fn) as fpr:
                rr = RecordReader(fpr)
                assert rr.read() == test_string_1
                assert rr.read() == test_string_2
예제 #28
0
    def test_basic_recordreader_dup_failure(self):
        fp = self.mox.CreateMock(FileLike)
        fp.mode = 'r+'
        fp.Error = FileLike.Error
        fp.dup().AndRaise(FileLike.Error)

        self.mox.ReplayAll()

        rr = RecordReader(fp)
        assert list(rr) == []
        self.mox.VerifyAll()
예제 #29
0
  def test_basic_recordreader_iter_failure(self):
    self.mox.StubOutWithMock(RecordIO.Reader, 'do_read')
    fp = self.mox.CreateMock(FileLike)
    fp.mode = 'r+'
    fp.dup().AndReturn(fp)
    RecordIO.Reader.do_read(fp, mox.IsA(StringCodec)).AndRaise(RecordIO.Error)
    fp.close()

    self.mox.ReplayAll()

    rr = RecordReader(fp)
    assert list(rr) == []
예제 #30
0
    def test_basic_recordreader_read(self):
        test_string = "hello world"
        with self.EphemeralFile('r') as fp:
            fn = fp.name

            rr = RecordReader(fp)
            assert rr.read() is None
            rr.close()

            with open(fn, 'w') as fpw:
                rw = RecordWriter(fpw)
                rw.write(test_string)

            with open(fn) as fpr:
                rr = RecordReader(fpr)
                assert rr.read() == test_string
예제 #31
0
  def test_basic_recordreader_read(self):
    test_string = "hello world"
    with self.EphemeralFile('r') as fp:
      fn = fp.name

      rr = RecordReader(fp)
      assert rr.read() is None
      rr.close()

      with open(fn, 'w') as fpw:
        rw = RecordWriter(fpw)
        rw.write(test_string)

      with open(fn) as fpr:
        rr = RecordReader(fpr)
        assert rr.read() == test_string
예제 #32
0
 def test_raises_if_initialized_with_nil_filehandle(self):
     with pytest.raises(RecordIO.InvalidFileHandle):
         RecordWriter(None)
     with pytest.raises(RecordIO.InvalidFileHandle):
         RecordReader(None)
예제 #33
0
def test_raises_on_nonfile():
  with pytest.raises(RecordIO.InvalidFileHandle):
    RecordWriter('/tmp/poop')
  with pytest.raises(RecordIO.InvalidFileHandle):
    RecordReader('/tmp/poop')
예제 #34
0
 def test_recordreader_fails_with_writeonly(self):
     for mode in ('a', 'w'):
         with self.EphemeralFile(mode) as fp:
             with pytest.raises(RecordIO.InvalidFileHandle):
                 RecordReader(fp)