Skip to content

Commit

Permalink
Address resourse release issues and locale dependent comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
kovrus committed Oct 21, 2015
1 parent 9145e48 commit ed2a3d2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 79 deletions.
104 changes: 53 additions & 51 deletions blob/src/test/java/io/crate/BlobHeadRequestHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,56 +47,58 @@ public class BlobHeadRequestHandlerTests extends CrateUnitTest {
@Test
public void testPutHeadChunkRunnableFileGrowth() throws Exception {
File file = File.createTempFile("test", "");
final FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(new byte[] { 0x65 });

UUID transferId = UUID.randomUUID();
BlobTransferTarget blobTransferTarget = mock(BlobTransferTarget.class);
TransportService transportService = mock(TransportService.class);
DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
DigestBlob digestBlob = mock(DigestBlob.class);
when(digestBlob.file()).thenReturn(file);

ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(EsExecutors.daemonThreadFactory("blob-head"));
try {
scheduledExecutor.schedule(new Runnable() {
@Override
public void run() {
try {
outputStream.write(new byte[] { 0x66, 0x67, 0x68, 0x69 });
} catch (IOException ex) {
//pass
try (final FileOutputStream outputStream = new FileOutputStream(file)) {
outputStream.write(new byte[]{0x65});

UUID transferId = UUID.randomUUID();
BlobTransferTarget blobTransferTarget = mock(BlobTransferTarget.class);
TransportService transportService = mock(TransportService.class);
DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
DigestBlob digestBlob = mock(DigestBlob.class);
when(digestBlob.file()).thenReturn(file);

ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(EsExecutors.daemonThreadFactory("blob-head"));
try {
scheduledExecutor.schedule(new Runnable() {
@Override
public void run() {
try {
outputStream.write(new byte[]{0x66, 0x67, 0x68, 0x69});
} catch (IOException ex) {
//pass
}
}
}
}, 800, TimeUnit.MILLISECONDS);
PutHeadChunkRunnable runnable = new PutHeadChunkRunnable(
digestBlob, 5, transportService, blobTransferTarget, discoveryNode, transferId
);

@SuppressWarnings("unchecked")
TransportFuture<TransportResponse.Empty> result = mock(TransportFuture.class);

when(transportService.submitRequest(
eq(discoveryNode),
eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK),
any(TransportRequest.class),
any(TransportRequestOptions.class),
eq(EmptyTransportResponseHandler.INSTANCE_SAME)
)).thenReturn(result);

runnable.run();

verify(blobTransferTarget).putHeadChunkTransferFinished(transferId);
verify(transportService, times(2)).submitRequest(
eq(discoveryNode),
eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK),
any(TransportRequest.class),
any(TransportRequestOptions.class),
eq(EmptyTransportResponseHandler.INSTANCE_SAME)
);
} finally {
scheduledExecutor.awaitTermination(1, TimeUnit.SECONDS);
scheduledExecutor.shutdownNow();
}, 800, TimeUnit.MILLISECONDS);
PutHeadChunkRunnable runnable = new PutHeadChunkRunnable(
digestBlob, 5, transportService, blobTransferTarget, discoveryNode, transferId
);

@SuppressWarnings("unchecked")
TransportFuture<TransportResponse.Empty> result = mock(TransportFuture.class);

when(transportService.submitRequest(
eq(discoveryNode),
eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK),
any(TransportRequest.class),
any(TransportRequestOptions.class),
eq(EmptyTransportResponseHandler.INSTANCE_SAME)
)).thenReturn(result);

runnable.run();

verify(blobTransferTarget).putHeadChunkTransferFinished(transferId);
verify(transportService, times(2)).submitRequest(
eq(discoveryNode),
eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK),
any(TransportRequest.class),
any(TransportRequestOptions.class),
eq(EmptyTransportResponseHandler.INSTANCE_SAME)
);
} finally {
scheduledExecutor.awaitTermination(1, TimeUnit.SECONDS);
scheduledExecutor.shutdownNow();
}
}
}

Expand All @@ -107,9 +109,9 @@ public void testPutHeadChunkRunnableFileDoesntGrow() throws Exception {

File file = File.createTempFile("test", "");
File notExisting = new File("./does/not/exist");
final FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(new byte[] { 0x65 });

try (final FileOutputStream outputStream = new FileOutputStream(file)) {
outputStream.write(new byte[]{0x65});
}
UUID transferId = UUID.randomUUID();
BlobTransferTarget transferTarget = mock(BlobTransferTarget.class);
TransportService transportService = mock(TransportService.class);
Expand Down
29 changes: 9 additions & 20 deletions sql-parser/src/main/java/io/crate/sql/tree/BooleanLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,33 @@
package io.crate.sql.tree;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

public class BooleanLiteral
extends Literal
{
public static final BooleanLiteral TRUE_LITERAL = new BooleanLiteral("true");
public static final BooleanLiteral FALSE_LITERAL = new BooleanLiteral("false");
public class BooleanLiteral extends Literal {
public static final BooleanLiteral TRUE_LITERAL = new BooleanLiteral(true);
public static final BooleanLiteral FALSE_LITERAL = new BooleanLiteral(false);

private final boolean value;

public BooleanLiteral(String value)
{
Preconditions.checkNotNull(value, "value is null");
Preconditions.checkArgument(value.toLowerCase().equals("true") || value.toLowerCase().equals("false"));

this.value = value.toLowerCase().equals("true");
private BooleanLiteral(boolean value) {
this.value = value;
}

public boolean getValue()
{
public boolean getValue() {
return value;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitBooleanLiteral(this, context);
}

@Override
public int hashCode()
{
public int hashCode() {
return Objects.hashCode(value);
}

@Override
public boolean equals(Object obj)
{
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion sql-parser/src/main/java/io/crate/sql/tree/Literal.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static Literal fromObject(Object value) {
literal = new LongLiteral(value.toString());
}
} else if (value instanceof Boolean) {
literal = new BooleanLiteral(value.toString());
literal = (Boolean) value ? BooleanLiteral.TRUE_LITERAL : BooleanLiteral.FALSE_LITERAL;
} else if (value instanceof Object[]) {
List<Expression> expressions = new ArrayList<>();
for (Object o : (Object[]) value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package io.crate.test.integration;


import com.google.common.base.Charsets;
import org.elasticsearch.common.io.Streams;

import java.io.ByteArrayOutputStream;
Expand All @@ -33,12 +32,6 @@

public class PathAccessor {

public static String stringFromPath(String path, Class<?> aClass) throws IOException {
return Streams.copyToString(new InputStreamReader(
getInputStream(path, aClass),
Charsets.UTF_8));
}

public static byte[] bytesFromPath(String path, Class<?> aClass) throws IOException {
try (InputStream is = getInputStream(path, aClass);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
Expand Down

0 comments on commit ed2a3d2

Please sign in to comment.