我创建了一个模块,允许我使用Java与splunk集成。我们通过maven使用1.2.1.0。
我的模块似乎工作得很好。但是,我想把junit测试放在它周围。我创建了一些测试,试图返回最近的事件,但是除非我睡了很长时间,否则我永远不会得到我刚刚输入的东西,通常是在它之前的事件。我也尝试了事件计数索引(我使用一个测试索引),但这也没有正确更新。有什么好方法可以让我验证和断言JUnit测试吗?
我在这个应用程序中使用spring,所以我有一个单例服务来完成这个日志记录。以下是服务实现:
/*
* Copyright (c) 2015 POS Portal, Inc.
* 180 Promenade Circle, Ste 215, Sacramento, CA 95834, USA
* All rights reserved.
*
* This software is the confidential and proprietary information
* of POS Portal, Inc.
*
*/
package com.posportal.splunk.impl;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.posportal.splunk.api.SplunkLog;
import com.posportal.splunk.enumeration.SplunkLoggingLevel;
import com.splunk.Receiver;
import com.splunk.Service;
import com.splunk.ServiceArgs;
/**
* Represents a splunk logging instance. This is a spring singleton for logging.
*
* We log to the index given, so it is acceptable to have a singleton on a per index basis.
*
* Splunk configuration including index name are injected via Spring.
* @author Michael Wenk
*
*/
public class SplunkLogImpl implements SplunkLog {
private static Logger log = LoggerFactory.getLogger(SplunkLogImpl.class);
private String host;
private String user;
private String password;
private String scheme;
private String indexName;
private int port;
private boolean disabled = false;
private Service splunksvc = null;
private Receiver receiver = null;
@Override
public void logMessage(String msg, SplunkLoggingLevel level) {
if (disabled) {
log.warn("Splunk system disabled. Splunk message would be: " + msg + " on level: " + level);
} else {
if (receiver == null) {
initService();
}
String formattedMessageData = formatMessage(msg, level);
receiver.log(indexName, formattedMessageData);
}
}
private String formatMessage(String msg, SplunkLoggingLevel level) {
String fmt = "timestamp=\"%s\",level=\"%s\",data=\"%s\"";
Date now = new Date();
return String.format(fmt, now.toString(), level.toString(), msg);
}
private void initService() {
ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setUsername(user);
loginArgs.setPassword(password);
loginArgs.setHost(host);
loginArgs.setScheme(scheme);
loginArgs.setPort(port);
splunksvc = Service.connect(loginArgs);
receiver = splunksvc.getReceiver();
}
public void setHost(String host) {
this.host = host;
}
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public void setPort(int port) {
this.port = port;
}
@Override
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
@Override
public boolean isDisabled() {
return disabled;
}
}下面是单元测试代码:
/*
* Copyright (c) 2015 POS Portal, Inc.
* 180 Promenade Circle, Ste 215, Sacramento, CA 95834, USA
* All rights reserved.
*
* This software is the confidential and proprietary information
* of POS Portal, Inc.
*
*/
package com.posportal.splunk.test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.posportal.splunk.api.SplunkLog;
import com.posportal.splunk.enumeration.SplunkLoggingLevel;
import com.splunk.Job;
import com.splunk.JobArgs;
import com.splunk.JobArgs.ExecutionMode;
import com.splunk.Service;
import com.splunk.ServiceArgs;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:pos-splunk-test-ctx.xml")
public class SplunkTest {
//private static Logger log = LoggerFactory.getLogger(SplunkTest.class);
@Resource(name = "splunkLog")
private SplunkLog splunk;
@Resource(name = "splunkConfig")
private Map<String,String> splunkConfig;
@Test
public void testInitialMessage() throws InterruptedException
{
if (splunk.isDisabled())
{
String msg = "This is my test";
splunk.logMessage(msg, SplunkLoggingLevel.INFO);
}
else
{
int startCount = getEventCountFromIndex();
String msg = "This is my test";
assertNotNull(splunk);
splunk.logMessage(msg, SplunkLoggingLevel.INFO);
Service svc = getService();
assertNotNull(svc);
// Sleep for a while.
Thread.sleep(4000);
int finalCount = getEventCountFromIndex();
assertTrue(finalCount > startCount);
}
}
@Test
public void testDisabled() {
if (!splunk.isDisabled())
{
splunk.setDisabled(true);
splunk.logMessage("This is a disabled test", SplunkLoggingLevel.INFO);
// Can't assert unfortunately,
//FIXME see if I can assert using log4j itself.
}
}
private int getEventCountFromIndex() {
String searchString = "search index="+ splunkConfig.get("indexName");
JobArgs jargs = new JobArgs();
jargs.setExecutionMode(ExecutionMode.BLOCKING);
Service svc = getService();
Job j = svc.getJobs().create(searchString, jargs);
return j != null ? j.getEventCount() : -1;
}
@Test
public void testSecondMessage() throws InterruptedException
{
if (splunk.isDisabled())
{
String msg = "This is my second test";
splunk.logMessage(msg, SplunkLoggingLevel.INFO);
}
else
{
int startCount = getEventCountFromIndex();
String msg = "This is my second test";
assertNotNull(splunk);
splunk.logMessage(msg, SplunkLoggingLevel.INFO);
Service svc = getService();
assertNotNull(svc);
// Sleep for a while.
Thread.sleep(4000);
int finalCount = getEventCountFromIndex();
assertTrue(finalCount > startCount);
}
}
private Service getService() {
ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setUsername(splunkConfig.get("user"));
loginArgs.setPassword(splunkConfig.get("password"));
loginArgs.setHost(splunkConfig.get("host"));
loginArgs.setScheme(splunkConfig.get("scheme"));
int port = Integer.parseInt(splunkConfig.get("port"));
loginArgs.setPort(port);
Service service = Service.connect(loginArgs);
return service;
}
}如果你注意到睡在里面的话。如果我不睡觉,指数中的事件#就不会增加。不幸的是,有时4秒是不够的。
发布于 2015-02-24 21:44:54
您看到的时间延迟是Splunk索引事件,这涉及到写入磁盘,可能需要时间。解决方案是,正如您已经尝试过的,等待Splunk完成。
我建议为您想尝试的次数设置一个上限,并每x秒检查一次(睡眠时间)。
在Splunk (参见SearchJobTest.java和SDKTestCase.java)中,我们对assertEventuallyTrue()方法做了同样的事情:
public static boolean assertEventuallyTrue(EventuallyTrueBehavior behavior) {
int remainingTries = behavior.tries;
while (remainingTries > 0) {
boolean succeeded = behavior.predicate();
if (succeeded) {
return true;
} else {
remainingTries -= 1;
try {
Thread.sleep(behavior.pauseTime);
} catch (InterruptedException e) {}
}
}
Assert.fail(behavior.timeoutMessage);
return false;
}发布于 2015-02-25 21:37:50
我想这是单元测试,所以您不应该在单元测试中集成splunk。也许你尝试的是接受测试。
对于单元测试,您需要从Splunk抽象出来。因此,只需从Splunk创建您自己的抽象层,或者模拟Splunk实现。
因此,您可以测试您的所有参数是否受到尊重并开始创建服务,还可以测试您的所有日志消息是否有足够好的格式化和发送到服务。
最后,这就是我们所需要做的。摘自或嘲弄它,你会没事的。
https://stackoverflow.com/questions/28619336
复制相似问题