//  [python]
def test_caching(lru_bytes):
  image = np.zeros(shape=(128,128,128,1), dtype=np.uint8)
  image[0:64,0:64,0:64] = 1
  image[64:128,0:64,0:64] = 2
  image[0:64,64:128,0:64] = 3
  image[0:64,0:64,64:128] = 4
  image[64:128,64:128,0:64] = 5
  image[64:128,0:64,64:128] = 6
  image[0:64,64:128,64:128] = 7
  image[64:128,64:128,64:128] = 8

  dirpath = f'/tmp/cloudvolume/caching-volume-{TEST_NUMBER}-{lru_bytes}'
  layer_path = 'file://' + dirpath

  vol = CloudVolume.from_numpy(
    image, 
    voxel_offset=(0,0,0), 
    vol_path=layer_path, 
    layer_type='image', 
    resolution=(1,1,1), 
    encoding='raw',
    chunk_size=(64,64,64),
  )
  vol.image.lru.resize(lru_bytes)
  vol.cache.enabled = True
  vol.cache.flush()

  # Test that reading populates the cache
  read1 = vol[:,:,:]
  assert n.all(read1 == image)

  read2 = vol[:,:,:]
  assert n.all(read2 == image)

  assert len(vol.cache.list()) > 0

  files = vol.cache.list()
  validation_set = [
    '0-64_0-64_0-64',
    '64-128_0-64_0-64',
    '0-64_64-128_0-64',
    '0-64_0-64_64-128',
    '64-128_64-128_0-64',
    '64-128_0-64_64-128',
    '0-64_64-128_64-128',
    '64-128_64-128_64-128'
  ]
  assert set([ os.path.splitext(fname)[0] for fname in files ]) == set(validation_set)

  for i in range(8):
    fname = os.path.join(vol.cache.path, vol.key, validation_set[i]) + '.gz'
    with gzip.GzipFile(fname, mode='rb') as gfile:
      chunk = gfile.read()
    img3d = chunks.decode(
      chunk, 'raw', (64,64,64,1), np.uint8
    )
    assert n.all(img3d == (i+1))

  vol.cache.flush()
  assert not os.path.exists(vol.cache.path)

  # Test that writing populates the cache
  vol[:,:,:] = image

  assert os.path.exists(vol.cache.path)
  assert n.all(vol[:,:,:] == image)

  vol.cache.flush()
  vol.image.lru.clear()

  # Test that partial reads work too
  result = vol[0:64,0:64,:]
  assert n.all(result == image[0:64,0:64,:])
  files = vol.cache.list()
  assert len(files) == 2
  result = vol[:,:,:]
  assert n.all(result == image)
  files = vol.cache.list()
  assert len(files) == 8

  vol.cache.flush()

  # Test Non-standard Cache Destination
  dirpath = f'/tmp/cloudvolume/caching-cache-{TEST_NUMBER}-{lru_bytes}'
  vol.cache.enabled = True
  vol.cache.path = dirpath
  vol[:,:,:] = image

  assert len(os.listdir(os.path.join(dirpath, vol.key))) == 8

  vol.cache.flush()

  # Test that caching doesn't occur when cache is not set
  vol.cache.enabled = False
  result = vol[:,:,:]
  if os.path.exists(vol.cache.path):
    files = vol.cache.list()
    assert len(files) == 0

  vol[:,:,:] = image
  if os.path.exists(vol.cache.path):
    files = vol.cache.list()
    assert len(files) == 0

  vol.cache.flush()

  # Test that deletion works too
  vol.cache.enabled = True
  vol[:,:,:] = image
  files = vol.cache.list()
  assert len(files) == 8
  vol.delete( np.s_[:,:,:] )
  files = vol.cache.list()
  assert len(files) == 0

  vol.cache.flush()    

  vol[:,:,:] = image
  files = vol.cache.list()
  assert len(files) == 8
  vol.cache.flush(preserve=np.s_[:,:,:])
  files = vol.cache.list()
  assert len(files) == 8
  vol.cache.flush(preserve=np.s_[:64,:64,:])
  files = vol.cache.list()
  assert len(files) == 2

  vol.cache.flush()

  vol[:,:,:] = image
  files = vol.cache.list()
  assert len(files) == 8
  vol.cache.flush_region(Bbox( (50, 50, 0), (100, 100, 10) ))
  files = vol.cache.list()
  assert len(files) == 4

  vol.cache.flush()

  vol[:,:,:] = image
  files = vol.cache.list()
  assert len(files) == 8
  vol.cache.flush_region(np.s_[50:100, 50:100, 0:10])
  files = vol.cache.list()
  assert len(files) == 4

  vol.cache.flush()