K8SFileChannel extends SeekableInMemoryByteChannel { @Override public void close() { try { ccf.executeCloudFunction(client -> createOrReplaceFSCM(client, file, createOrReplaceParentDirFSCM(client, file, size(), false), Collections.singletonMap(CFG_MAP_FSOBJ_CONTENT_KEY, super.toString()), false), KubernetesClient.class); } finally { this.ccf = null; this.file = null; super.close(); } } K8SFileChannel(Path file, CloudClientFactory ccf); @Override void close();  }
@Test public void testOpenAndCloseChannel() { final K8SFileSystem kfs = (K8SFileSystem) fsProvider.getFileSystem(URI.create("k8s: final Path valid = kfs.getPath("/test"); K8SFileChannel k8sfc = new K8SFileChannel(valid, (CloudClientFactory) fsProvider); assertTrue(k8sfc.isOpen()); k8sfc.close(); assertFalse(k8sfc.isOpen()); }
@Test public void testChannelMustBeClosedRegardlessError() { FileSystemProvider fsProvider = new K8SFileSystemProvider() { @Override public KubernetesClient createKubernetesClient() { return CLIENT_FACTORY.get(); } }; final K8SFileSystem kfs = (K8SFileSystem) fsProvider.getFileSystem(URI.create("k8s: final Path valid = kfs.getPath("/test"); K8SFileChannel k8sfc = new K8SFileChannel(valid, (CloudClientFactory) fsProvider); assertTrue(k8sfc.isOpen()); k8sfc.ccf = null; try { k8sfc.close(); fail("Channel close should throw an exception."); } catch (Exception e) { } assertFalse(k8sfc.isOpen()); }