Is there a way to set test timeout value dynamically - unit-testing

Currently I am using the following command for running my test with timeout value given during test call.
go test myModule -run TestSanity -v --race -timeout 10h
Is there a way in Golang testing module to set it during program execution.
Something like,
func TestMain(m *testing.M) {
// customTimeout = "10h"
// m.Timeout(customTimeout) <--- Something like this
code := m.Run()
os.Exit(code)
}

You could write your own function to do that:
func panicOnTimeout(d time.Duration) {
<-time.After(d)
panic("Test timed out")
}
func TestMain(m *testing.M) {
go panicOnTimeout(10 * time.Hour) // custom timeout
code := m.Run()
os.Exit(code)
}
This should simulate what go test -timeout does. Be sure to pass -timeout 0 to prevent the default test timeout from triggering though.

Related

Testing logs printed using other external library in golang

My application uses other external library (which uses zap library) to print audit-logs and now I want to test the printed audit-logs in my golang testing file.
Can someone help me with it.
You could take a look at the zaptest package:
Package zaptest provides a variety of helpers for testing log output.
As example, in your test:
func Test_zap(t *testing.T) {
t.Run("Handle log message", func(t *testing.T) {
// Given
observedZapCore, observedLogs := observer.New(zap.InfoLevel)
observedLogger := zap.New(observedZapCore)
// When
myFunction(observedLogger)
// Then
require.Equal(t, 1, observedLogs.Len())
firstLog := observedLogs.All()[0]
assert.Equal(t, "log myFunction", firstLog.Message)
})
}
where myfunction is
func myFunction(logger *zap.Logger) {
logger.Info("log myFunction")
}
Check also this interesting article about that

actix web test doesn't seem to be routing requests as expected

I recently updated to actix web 4, I had some tests that used the actix-web test module that stopped working as expected in the process. I'm sure it's something simple but I'm having trouble figuring out what changed. Here is a minimal example of the issue:
use actix_web::{test, web, App, HttpResponse, HttpRequest};
#[actix_rt::test]
async fn says_hello() {
let req = test::TestRequest::get().uri("/index.html").to_request();
let mut server =
test::init_service(App::new().service(web::scope("/").route("index.html", web::get().to(|_req: HttpRequest| async {
println!("Hello?");
HttpResponse::Ok()
})))).await;
let _resp = test::call_and_read_body(&mut server, req).await;
}
running this test I would expect to see "Hello?" output to my console, however, the request handler function I have defined at "/index.html" doesn't seem to be called and I receive no output.
To be clear, the tests are more complicated and have assertions etc, this is just a working example of the main issue I am trying to resolve
actix-web = { version = "4.1.0", default-features = false }
note:
if I change all paths to the root path it will call the handler, I.E.
let req = test::TestRequest::get().uri("/").to_request();
let mut server =
test::init_service(App::new().service(web::scope("/").route("/", web::get().to(|_req: HttpRequest| async {
println!("Hello?");
HttpResponse::Ok()
})))).await;
let _resp = test::call_and_read_body(&mut server, req).await;
// prints "Hello?" to the console
However no other route combination I have tried calls the request handler.
Rust tests capture the output and only output them for failed tests.
If you want to show output on all tests you have to tell them to do so with either testbinary --nocapture or cargo test -- --nocapture.
I was able to make things work by changing the path in the scope to an empty string
let req = test::TestRequest::get().uri("/index.html").to_request();
let mut server =
test::init_service(App::new().service(web::scope("").route("index.html", web::get().to(|_req: HttpRequest| async {
println!("Hello?");
HttpResponse::Ok()
})))).await;
let _resp = test::call_and_read_body(&mut server, req).await;
// prints "Hello?"

Is there any option to use something similar to mockito argument captor?

we are using gopkg.in/mgo.v2/bson to talk with mongo, and its API populates passed structures instead returning results, for example:
func (p *Pipe) One(result interface{}) error {...
Problems occurs when I want to mock / test code which is using that. I want to both mock this execution and somehow get pupulated value in 'result'.
Currently test has:
query.EXPECT().One(gomock.Any())
So as you can see I dont get any value, I just configure gomock to check that when I run my method then query.One has to be called.
I cannot pass structure like
mystruct := MyStruct{}
query.EXPECT().One(&mystruct)
because mystruct in test code and in real code is different and verifing mock will fail (references are different). Im looking for something similar to mockito's argument captor:
https://static.javadoc.io/org.mockito/mockito-core/2.6.9/org/mockito/ArgumentCaptor.html
This can be achieved via Do.
Copy & Paste of Github example from poy.
var capturedArgs []int
someMock.
EXPECT().
SomeMethod(gomock.Any()).
Do(func(arg int){
capturedArgs = append(capturedArgs, arg)
})
Ref: https://github.com/golang/mock/pull/149
This project can help you: https://github.com/bouk/monkey.
You can replace a function and use a bool variable to check the use.
called := false
monkey.Patch(package.One, func(result interface{}) error {
if result == expected {
called := true
return nil
}
return errors.new("not expected")
})
Dont't forget to restore your original function.
defer monkey.Unpatch(package.One)

httptest.ResponseRecorder has no field or method Result

So this one is really weird, I'm trying to get a mock response that renders JSON. My test looks like this:
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
...
func TestMessageFromResponse(t *testing.T) {
mc := MyController{}
body := "{ \"message\":\"kthxbai\" }"
w := httptest.NewRecorder()
w.Write([]byte(body))
resp := w.Result()
msg := mc.messageFromResponse(resp)
if msg != "kthxbai" {
t.Errorf("Expected response body to be kthxbai but was %s", msg)
}
}
I'm testing the method messageFromResponse on MyControllerbut its not even building. When I run go test in my project directory, I get the following error:
./my_controller_test.go:115: w.Result undefined (type *httptest.ResponseRecorder has no field or method Result)
I should also mention that I am successfully using httptest.ResponseRecorder as a writer stub elsewhere in this same file, but it's just failing when I try to access Result().
I initially addressed this as a comment because I was unsure of relevance, but for posterity:
The online godoc always references the latest release. In this case, the Result() method was added in Go1.7 which only released last week. When in doubt, check your local godoc by running godoc -server or godoc <packagename>.

Program termination during quartz scheduler verification using power mockito

I am trying to verify the quartz scheduler process by mocking System.currentTimeMillis() using PowerMockito .
Scenario :
I have scheduled a job for May 6th 2016 12 hrs .For verification I am trying to mock the System.currentTimeMillis() to scheduled time. So, the quartz will trigger on that particular time.
Test Case :
#RunWith(PowerMockRunner.class)
#PrepareForTest({ QuartzSchedulerThread.class })
public class SampleExampleTest {
#Test
public void test() {
/**
* mock the system.current milliseconds
*/
PowerMockito.mockStatic(System.class);
long value=getMockedMilliseconds();
PowerMockito.when(System.currentTimeMillis()).thenReturn(value);
System.out.println("Mocked milliseconds"+value);
try {
SchedulerFactory sf = new StdSchedulerFactory(getProperties());
Scheduler sched = sf.getScheduler();
sched.start();
} catch (SchedulerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public long getMockedMilliseconds() {
Date expectedDate=new Date(116, 4, 6 , 11, 57);
long mokedMilliSeconds = expectedDate.getTime();
return mokedMilliSeconds;
}
public Properties getProperties(){
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true");
// set other properties ...such as
properties.setProperty("org.quartz.jobStore.class",
"org.quartz.impl.jdbcjobstore.JobStoreTX");
properties.setProperty("org.quartz.jobStore.driverDelegateClass",
"org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");
properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
properties.setProperty("org.quartz.jobStore.dataSource", "obulis");
//properties.setProperty("org.quartz.jobStore.misfireThreshold", "1000");
// Datasource configurations
properties.setProperty("org.quartz.dataSource.obulis.driver",
"org.postgresql.Driver");
properties.setProperty("org.quartz.dataSource.obulis.URL",
"jdbc:postgresql://192.168.27.43:5433/obulis");
properties.setProperty("org.quartz.dataSource.obulis.user", "postgres");
properties.setProperty("org.quartz.dataSource.obulis.password",
"Nuwaza123");
properties.setProperty("org.quartz.dataSource.obulis.maxConnections",
"5");
properties.setProperty("rg.quartz.dataSource.obulis.validationQuery",
"select 0");
properties.setProperty("org.quartz.threadPool.class",
"org.quartz.simpl.SimpleThreadPool");
properties.setProperty("org.quartz.threadPool.threadCount", "4");
return properties;
}
}
Problem:
While executing the test case the program terminates abruptly before starting the scheduler. If the PowerMock is removed it is starting the scheduler.
console:
Mocked milliseconds1462516020000
INFO [main] (MLog.java:80) - MLog clients using log4j logging.
INFO [main] (C3P0Registry.java:204) - Initializing c3p0-0.9.1.1 [built 15- March-2007 01:32:31; debug? true; trace: 10]
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
What will be the cause of it? How to mock and verify whether the job will trigger on that particular time ?
I think you're going down a very bad way... You shouldn't mock the types you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-type-you-dont-own .
What exactly do you want to verify? That Quartz is working? Makes no sense - if it didn't the library wouldn't be released ;) If you do want to plug it in in the integration test, just run a normal Quartz job. Let that job do sth (produce an output) and then check if this output is present.