-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test InternalSampler as well as CLISampler
- Loading branch information
Jaroslav Tulach
committed
Apr 29, 2022
1 parent
c8faa73
commit d66ce57
Showing
5 changed files
with
274 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
platform/sampler/test/unit/src/org/netbeans/modules/sampler/AbstractSamplerBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.netbeans.modules.sampler; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.Closeable; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.util.logging.Handler; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
import java.util.logging.Logger; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
import org.junit.Test; | ||
import org.openide.util.Exceptions; | ||
|
||
public abstract class AbstractSamplerBase { | ||
|
||
protected abstract SamplerTest.Handle createManualSampler(String name); | ||
protected abstract SamplerTest.Handle createSampler(String name); | ||
protected abstract boolean logsMessage(); | ||
|
||
protected static abstract class Handle { | ||
protected abstract void start(); | ||
protected abstract void stop(); | ||
protected abstract void stopAndWriteTo(DataOutputStream dos) throws IOException; | ||
protected abstract void cancel(); | ||
} | ||
|
||
final void longRunningMethod() { | ||
for (int i = 0; i < 100; i++) { | ||
try { | ||
Thread.sleep(30); | ||
} catch (InterruptedException ex) { | ||
Exceptions.printStackTrace(ex); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Test of cancel method, of class Sampler. | ||
*/ | ||
@Test | ||
public void testCancel() { | ||
SamplerTest.Handle instance = createManualSampler("cancel"); | ||
instance.start(); | ||
instance.cancel(); | ||
} | ||
|
||
/** | ||
* Test of createManualSampler method, of class Sampler. | ||
*/ | ||
@Test | ||
public void testCreateManualSampler() { | ||
String name = "gentest"; | ||
SamplerTest.Handle result = createManualSampler(name); | ||
assertNotNull(result); | ||
} | ||
|
||
/** | ||
* Test of createSampler method, of class Sampler. | ||
*/ | ||
@Test | ||
public void testCreateSampler() { | ||
String name = "test"; | ||
SamplerTest.Handle result = createSampler(name); | ||
assertNotNull(result); | ||
} | ||
|
||
/** | ||
* Test of stop method, of class Sampler. | ||
*/ | ||
@Test | ||
public void testStop() { | ||
SamplerTest.Handle instance = createManualSampler("stop"); | ||
assertNotNull(instance); | ||
try (final SamplerTest.DD d = new SamplerTest.DD()) { | ||
instance.start(); | ||
longRunningMethod(); | ||
instance.stop(); | ||
if (logsMessage()) { | ||
assertNotNull("Cancel message has been logged", d.logged); | ||
} else { | ||
assertNull("CLI handler doesn't use logging", d.logged); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Test of stopAndWriteTo method, of class Sampler. | ||
*/ | ||
@Test | ||
public void testStopAndWriteTo() throws IOException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
try (DataOutputStream dos = new DataOutputStream(out)) { | ||
SamplerTest.Handle instance = createSampler("cancel"); | ||
instance.start(); | ||
instance.stopAndWriteTo(dos); | ||
} | ||
// there should no data in out, since stopAndWriteTo is | ||
// invoked immediately after start | ||
assertTrue(out.size() == 0); | ||
} | ||
|
||
/** | ||
* Test of stopAndWriteTo method, of class Sampler. | ||
*/ | ||
@Test | ||
public void testStopAndWriteTo1() throws IOException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
try (DataOutputStream dos = new DataOutputStream(out)) { | ||
SamplerTest.Handle instance = createSampler("cancel"); | ||
instance.start(); | ||
longRunningMethod(); | ||
instance.stopAndWriteTo(dos); | ||
} | ||
// make sure we have some sampling data | ||
assertTrue(out.size() > 0); | ||
} | ||
|
||
public static final class DD extends Handler implements Closeable { | ||
|
||
private final Logger LOG = Logger.getLogger("org.openide.util.Exceptions"); | ||
LogRecord logged; | ||
|
||
public DD() { | ||
super(); | ||
LOG.addHandler(this); | ||
LOG.setLevel(Level.WARNING); | ||
LOG.setUseParentHandlers(false); | ||
setLevel(Level.WARNING); | ||
} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
logged = record; | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
} | ||
|
||
@Override | ||
public void close() throws SecurityException { | ||
LOG.removeHandler(this); | ||
} | ||
} // end of DD | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
platform/sampler/test/unit/src/org/netbeans/modules/sampler/CLISampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.netbeans.modules.sampler; | ||
|
||
import java.io.IOException; | ||
import java.lang.management.ManagementFactory; | ||
|
||
public class CLISampleTest extends AbstractSamplerBase { | ||
@Override | ||
protected Handle createManualSampler(String name) { | ||
CLISampler sampler = new CLISampler(ManagementFactory.getThreadMXBean(), null) { | ||
@Override | ||
protected void saveSnapshot(byte[] arr) throws IOException { | ||
} | ||
}; | ||
return new DirectSamplerHandle(sampler); | ||
} | ||
|
||
@Override | ||
protected Handle createSampler(String name) { | ||
return createManualSampler(name); | ||
} | ||
|
||
@Override | ||
protected boolean logsMessage() { | ||
return false; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
platform/sampler/test/unit/src/org/netbeans/modules/sampler/DirectSamplerHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.netbeans.modules.sampler; | ||
|
||
import java.io.DataOutputStream; | ||
|
||
final class DirectSamplerHandle extends AbstractSamplerBase.Handle { | ||
private final Sampler sampler; | ||
|
||
DirectSamplerHandle(Sampler sampler) { | ||
this.sampler = sampler; | ||
} | ||
|
||
@Override | ||
public final synchronized void start() { | ||
sampler.start(); | ||
} | ||
|
||
@Override | ||
public final void cancel() { | ||
sampler.cancel(); | ||
} | ||
|
||
@Override | ||
public final void stopAndWriteTo(DataOutputStream dos) { | ||
sampler.stopAndWriteTo(dos); | ||
} | ||
|
||
@Override | ||
public final void stop() { | ||
sampler.stop(); | ||
} | ||
} |
Oops, something went wrong.