Skip to content

Commit

Permalink
[TEST] Improve HotThreadsRequest BWC test (#77818) (#77886)
Browse files Browse the repository at this point in the history
Backport of #77818

This change is addressing the test improvement comment for #77462.
Instead of the manual serialization, the test is now using VersionUtils
to serialize and deserialize the same request using different versions.
  • Loading branch information
grcevski authored Sep 16, 2021
1 parent 213c4d4 commit 09fca4e
Showing 1 changed file with 40 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,61 @@

package org.elasticsearch.action.admin.cluster.hotthreads;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest;
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.monitor.jvm.HotThreads;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class NodesHotThreadsRequestTests extends ESTestCase {

/** Simple override of BaseNodesRequest to ensure we read the
* common fields of the nodes request.
*/
static class NodesHotThreadsRequestHelper extends BaseNodesRequest<NodesHotThreadsRequestHelper> {
NodesHotThreadsRequestHelper(StreamInput in) throws IOException {
super(in);
}

NodesHotThreadsRequestHelper(String... nodesIds) {
super(nodesIds);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
}
}

public void testBWCSerialization() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();

TimeValue sampleInterval = new TimeValue(50, TimeUnit.MINUTES);

NodesHotThreadsRequestHelper outHelper = new NodesHotThreadsRequestHelper("123");

outHelper.writeTo(out);
// Write manually some values that differ from the defaults
// in NodesHotThreadsRequest
out.writeInt(4); // threads
out.writeBoolean(false); // ignoreIdleThreads
out.writeString("block"); // type
out.writeTimeValue(sampleInterval); // interval
out.writeInt(3); // snapshots

NodesHotThreadsRequest inRequest = new NodesHotThreadsRequest(out.bytes().streamInput());

assertEquals(4, inRequest.threads());
assertFalse(inRequest.ignoreIdleThreads());
assertEquals(HotThreads.ReportType.BLOCK, inRequest.type());
assertEquals(sampleInterval, inRequest.interval());
assertEquals(3, inRequest.snapshots());

// Change the report type enum
inRequest.type(HotThreads.ReportType.WAIT);

BytesStreamOutput writeOut = new BytesStreamOutput();
inRequest.writeTo(writeOut);

StreamInput whatWeWrote = writeOut.bytes().streamInput();
NodesHotThreadsRequest request = new NodesHotThreadsRequest("123");
request.threads(4);
request.ignoreIdleThreads(false);
request.type(HotThreads.ReportType.BLOCK);
request.interval(sampleInterval);
request.snapshots(3);

Version latest = Version.CURRENT;
Version previous = VersionUtils.randomVersionBetween(random(),
VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.CURRENT));

try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(latest);
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
in.setVersion(previous);
NodesHotThreadsRequest deserialized = new NodesHotThreadsRequest(in);
assertEquals(request.threads(), deserialized.threads());
assertEquals(request.ignoreIdleThreads(), deserialized.ignoreIdleThreads());
assertEquals(request.type(), deserialized.type());
assertEquals(request.interval(), deserialized.interval());
assertEquals(request.snapshots(), deserialized.snapshots());

}
}

// We construct the helper to read the common serialized fields from the in.
new NodesHotThreadsRequestHelper(whatWeWrote);
// Make sure we serialized in the following format
assertEquals(4, whatWeWrote.readInt());
assertFalse(whatWeWrote.readBoolean());
assertEquals("wait", whatWeWrote.readString()); // lowercase enum value, not label
assertEquals(sampleInterval, whatWeWrote.readTimeValue());
assertEquals(3, whatWeWrote.readInt());
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(previous);
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
in.setVersion(latest);
NodesHotThreadsRequest deserialized = new NodesHotThreadsRequest(in);
assertEquals(request.threads(), deserialized.threads());
assertEquals(request.ignoreIdleThreads(), deserialized.ignoreIdleThreads());
assertEquals(request.type(), deserialized.type());
assertEquals(request.interval(), deserialized.interval());
assertEquals(request.snapshots(), deserialized.snapshots());
}
}
}
}

0 comments on commit 09fca4e

Please sign in to comment.