Exemplo n.º 1
0
    def test_memory_manager(self):
        slide_man = SlidingWindowMapManager()
        static_man = StaticWindowMapManager()

        for man in (static_man, slide_man):
            assert man.num_file_handles() == 0
            assert man.num_open_files() == 0
            winsize_cmp_val = 0
            if isinstance(man, StaticWindowMapManager):
                winsize_cmp_val = -1
            # END handle window size
            assert man.window_size() > winsize_cmp_val
            assert man.mapped_memory_size() == 0
            assert man.max_mapped_memory_size() > 0

            # collection doesn't raise in 'any' mode
            man._collect_lru_region(0)
            # doesn't raise if we are within the limit
            man._collect_lru_region(10)

            # doesn't fail if we over-allocate
            assert man._collect_lru_region(sys.maxsize) == 0

            # use a region, verify most basic functionality
            with FileCreator(self.k_window_test_size, "manager_test") as fc:
                fd = os.open(fc.path, os.O_RDONLY)
                try:
                    for item in (fc.path, fd):
                        c = man.make_cursor(item)
                        assert c.path_or_fd() is item
                        assert c.use_region(10, 10).is_valid()
                        assert c.ofs_begin() == 10
                        assert c.size() == 10
                        with open(fc.path, 'rb') as fp:
                            assert c.buffer()[:] == fp.read(20)[10:]

                    if isinstance(item, int):
                        self.assertRaises(ValueError, c.path)
                    else:
                        self.assertRaises(ValueError, c.fd)
                    # END handle value error
                # END for each input
                finally:
                    os.close(fd)
Exemplo n.º 2
0
from .lib import TestBase, FileCreator

from smmap.mman import (SlidingWindowMapManager, StaticWindowMapManager)
from smmap.buf import SlidingWindowMapBuffer

from random import randint
from time import time
import sys
import os

man_optimal = SlidingWindowMapManager()
man_worst_case = SlidingWindowMapManager(
    window_size=TestBase.k_window_test_size // 100,
    max_memory_size=TestBase.k_window_test_size // 3,
    max_open_handles=15)
static_man = StaticWindowMapManager()


class TestBuf(TestBase):
    def test_basics(self):
        fc = FileCreator(self.k_window_test_size, "buffer_test")

        # invalid paths fail upon construction
        c = man_optimal.make_cursor(fc.path)
        self.assertRaises(ValueError, SlidingWindowMapBuffer,
                          type(c)())  # invalid cursor
        self.assertRaises(ValueError, SlidingWindowMapBuffer, c,
                          fc.size)  # offset too large

        buf = SlidingWindowMapBuffer()  # can create uninitailized buffers
        assert buf.cursor() is None