Skip to content

Commit d20db18

Browse files
fixed unit test
Signed-off-by: Matt Peterson <[email protected]>
1 parent bb4c5af commit d20db18

File tree

6 files changed

+85
-27
lines changed

6 files changed

+85
-27
lines changed

server/src/main/java/com/hedera/block/server/ServiceStatus.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.hedera.block.server;
218

319
import io.helidon.webserver.WebServer;

server/src/main/java/com/hedera/block/server/mediator/LiveStreamMediatorImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.lmax.disruptor.RingBuffer;
2727
import com.lmax.disruptor.dsl.Disruptor;
2828
import com.lmax.disruptor.util.DaemonThreadFactory;
29-
import io.helidon.webserver.WebServer;
3029
import java.io.IOException;
3130
import java.util.Map;
3231
import java.util.concurrent.ConcurrentHashMap;

server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirWriter.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,34 @@ public void write(final BlockItem blockItem) throws IOException {
8282
}
8383

8484
final Path blockItemFilePath = calculateBlockItemPath();
85-
for (int retries = 0;;retries++) {
85+
for (int retries = 0; ; retries++) {
8686
try {
8787
write(blockItemFilePath, blockItem);
8888
break;
8989
} catch (IOException e) {
9090

91-
LOGGER.log(System.Logger.Level.ERROR, "Error writing the BlockItem protobuf to a file", e);
92-
if (retries > 1) {
91+
LOGGER.log(
92+
System.Logger.Level.ERROR,
93+
"Error writing the BlockItem protobuf to a file",
94+
e);
95+
if (retries > 0) {
9396
// Attempt to remove the block
94-
blockRemover.remove(blockNodeFileNameIndex);
97+
blockRemover.remove(Long.parseLong(currentBlockDir.toString()));
9598
throw e;
9699
} else {
97100
// Attempt to repair the permissions on the block path
98101
// and the blockItem path
102+
repairPermissions(blockNodeRootPath);
99103
repairPermissions(calculateBlockPath());
100-
LOGGER.log(System.Logger.Level.INFO, "Retrying to write the BlockItem protobuf to a file");
104+
LOGGER.log(
105+
System.Logger.Level.INFO,
106+
"Retrying to write the BlockItem protobuf to a file");
101107
}
102108
}
103109
}
104110
}
105111

106-
private void write(final Path blockItemFilePath, final BlockItem blockItem) throws IOException {
112+
void write(final Path blockItemFilePath, final BlockItem blockItem) throws IOException {
107113
try (FileOutputStream fos = new FileOutputStream(blockItemFilePath.toString())) {
108114
blockItem.writeTo(fos);
109115
LOGGER.log(
@@ -133,7 +139,7 @@ private void resetState(final BlockItem blockItem) throws IOException {
133139
blockNodeFileNameIndex = 0;
134140
}
135141

136-
private void repairPermissions(final Path path) throws IOException{
142+
private void repairPermissions(final Path path) throws IOException {
137143
final boolean isReadable = Files.isReadable(path);
138144
final boolean isWritable = Files.isWritable(path);
139145
if (!isWritable || !isReadable) {
@@ -157,7 +163,7 @@ private void repairPermissions(final Path path) throws IOException{
157163
} catch (IOException e) {
158164
LOGGER.log(
159165
System.Logger.Level.ERROR,
160-
"Error setting permissions on the block node root directory",
166+
"Error setting permissions on the path: " + path,
161167
e);
162168
throw e;
163169
}

server/src/main/java/com/hedera/block/server/producer/ProducerBlockItemObserver.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.hedera.block.server.data.ObjectEvent;
2424
import com.hedera.block.server.mediator.StreamMediator;
2525
import io.grpc.stub.StreamObserver;
26-
import io.helidon.webserver.WebServer;
2726
import java.io.IOException;
2827
import java.security.NoSuchAlgorithmException;
2928

server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
import java.io.IOException;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
32+
import java.nio.file.attribute.FileAttribute;
33+
import java.nio.file.attribute.PosixFilePermission;
3234
import java.util.List;
3335
import java.util.Map;
3436
import java.util.Optional;
37+
import java.util.Set;
3538
import org.junit.jupiter.api.AfterEach;
3639
import org.junit.jupiter.api.BeforeEach;
37-
import org.junit.jupiter.api.Disabled;
3840
import org.junit.jupiter.api.Test;
3941

4042
public class BlockAsDirectoryTest {
@@ -192,18 +194,19 @@ public void testRemoveBlockWritePerms() throws IOException {
192194
}
193195

194196
@Test
195-
@Disabled
196197
public void testRemoveBlockItemWritePerms() throws IOException {
197198

198199
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
199-
final BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
200-
201-
// Writing the header BlockItem will create the block directory.
202-
blockWriter.write(blockItems.get(0));
200+
final BlockRemover blockRemover =
201+
new BlockAsDirRemover(
202+
Path.of(testConfig.get(JUNIT).asString().get()), Util.defaultPerms);
203+
final BlockWriter<BlockItem> blockWriter =
204+
new TestIOExceptionBlockAsDirWriter(
205+
JUNIT, testConfig, blockRemover, Util.defaultPerms, 0);
203206

204-
// Change the permissions on the block node root directory
205-
removeBlockWritePerms(1, testConfig);
206-
assertThrows(IOException.class, () -> blockWriter.write(blockItems.get(1)));
207+
// TestBlockAsDirWriter overrides the write method to always
208+
// throw an IOException
209+
assertThrows(IOException.class, () -> blockWriter.write(blockItems.get(0)));
207210
}
208211

209212
@Test
@@ -215,21 +218,27 @@ public void testConstructorWithInvalidPath() {
215218
}
216219

217220
@Test
218-
@Disabled
219221
public void testPartialBlockRemoval() throws IOException {
220222
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
221-
final BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
223+
final BlockRemover blockRemover =
224+
new BlockAsDirRemover(
225+
Path.of(testConfig.get(JUNIT).asString().get()), Util.defaultPerms);
226+
final BlockWriter<BlockItem> blockWriter =
227+
new TestIOExceptionBlockAsDirWriter(
228+
JUNIT, testConfig, blockRemover, Util.defaultPerms, 3);
222229

223230
// Write a few block items
224231
blockWriter.write(blockItems.get(0));
225232
blockWriter.write(blockItems.get(1));
226233
blockWriter.write(blockItems.get(2));
227234

228-
// Remove write permissions from the block item
229-
removeBlockWritePerms(1, testConfig);
235+
// Attempt to write the next block should fail
236+
assertThrows(IOException.class, () -> blockWriter.write(blockItems.get(3)));
230237

231-
// Attempt to read the block
232-
assertThrows(IOException.class, () -> blockWriter.write(blockItems.get(4)));
238+
// Verify the partially written block was removed
239+
final BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
240+
final Optional<Block> blockOpt = blockReader.read(1);
241+
assertTrue(blockOpt.isEmpty());
233242
}
234243

235244
private void removeBlockReadPerms(int blockNumber, final Config config) throws IOException {
@@ -264,4 +273,31 @@ private void removeBlockItemReadPerms(int blockNumber, int blockItem, Config con
264273
final Path blockItemPath = blockPath.resolve(blockItem + BLOCK_FILE_EXTENSION);
265274
Files.setPosixFilePermissions(blockItemPath, TestUtils.getNoRead().value());
266275
}
276+
277+
static class TestIOExceptionBlockAsDirWriter extends BlockAsDirWriter {
278+
279+
final int blockItemsToAdmitBeforeFailure;
280+
private int currentCount;
281+
282+
public TestIOExceptionBlockAsDirWriter(
283+
final String key,
284+
final Config config,
285+
final BlockRemover blockRemover,
286+
final FileAttribute<Set<PosixFilePermission>> filePerms,
287+
final int blockItemsToAdmitBeforeFailure)
288+
throws IOException {
289+
super(key, config, blockRemover, filePerms);
290+
this.blockItemsToAdmitBeforeFailure = blockItemsToAdmitBeforeFailure;
291+
}
292+
293+
@Override
294+
void write(final Path blockItemFilePath, final BlockItem blockItem) throws IOException {
295+
if (currentCount < blockItemsToAdmitBeforeFailure) {
296+
super.write(blockItemFilePath, blockItem);
297+
currentCount++;
298+
} else {
299+
throw new IOException("Test exception");
300+
}
301+
}
302+
}
267303
}

server/src/test/java/com/hedera/block/server/producer/ProducerBlockItemObserverTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.hedera.block.server.persistence.storage.BlockReader;
3636
import com.hedera.block.server.persistence.storage.BlockWriter;
3737
import io.grpc.stub.StreamObserver;
38-
import io.helidon.webserver.WebServer;
3938
import java.io.IOException;
4039
import java.security.NoSuchAlgorithmException;
4140
import java.util.List;
@@ -197,7 +196,10 @@ public void testItemAckBuilderExceptionTest()
197196

198197
ProducerBlockItemObserver producerBlockItemObserver =
199198
new ProducerBlockItemObserver(
200-
streamMediator, publishStreamResponseObserver, itemAckBuilder, serviceStatus);
199+
streamMediator,
200+
publishStreamResponseObserver,
201+
itemAckBuilder,
202+
serviceStatus);
201203

202204
when(streamMediator.isPublishing()).thenReturn(true);
203205
when(itemAckBuilder.buildAck(any()))

0 commit comments

Comments
 (0)