Compiler optimization causes original static final value to be used even when it's changed by JMockit - jmockit

Consider the following code that uses JSch to create an SSH connection:
public class DoSsh {
private static final int DEFAULT_PORT = 22;
public DoSsh(String user, String pass) {
JSch jsch = new JSch();
Session sess = jsch.getSession(user, pass, DEFAULT_PORT);
...
And the following test code that uses JMockit:
#Test
public void testDoShs() {
// Change the default port
Deencapsulation.setField(DoSsh.class, "DEFAULT_PORT", 2222);
DoSsh ssh = new DoSsh("me","mypass");
...
The goal here is to cause the SSH connection to use an alternate port during test (2222 in this case) to connect to an in-memory SSH server (Apache MIRA).
When I debug this, I can see that the value of 'DEFAULT_PORT' has indeed been changed (thank you JMockit :-) The problem is that compiler has already optimized the call to 'jsch.getSession' and hard-coded the original value of 22 into it. So when I step into that call in the debugger, even though the value being passed in is 2222, the value inside the call is 22.
My question is, can anyone suggest a way to solve this that doesn't involve making DEFAULT_PORT non-final?

Found my own answer. It involves mocking out the call to 'jsch.getSession', but then calling the real version from within the mock, with the desired port number. This is basically an AOP approach. Deencapsulation is not used. Here's the code:
#MockClass(realClass = JSch.class)
public static class MockedJSch {
public JSch it;
#Mock(reentrant = true)
public Session getSession(final String user, final String pass, final int port) throws JSchException {
return it.getSession(user, pass, TESTING_PORT);
}
}
#BeforeMethod
public void beforeMethod() {
Mockit.setUpMocks(MockedJSch.class);
}
There are two key points to note here.
The mocked method is marked as 'reentrant'.
The mock has a public instance member called "it" that is used to call the "real" method. That instance member is initialized somewhere in the bowels of JMockit to refer to the instance upon which this method is invoked, and that reference has access to the "real" version of the method.

Related

Is there a way to pass redis commands in jedis, without using the functions?

We are trying to build a console to process redis queries. But, in the back end we need to use Jedis. So, the commands, given as the inputs needs to be processed using Jedis. For example, in redis-cli, we use " keys * ". For the same we use jedis.keys(" * ") in Jedis. I have no idea, how to convert " keys * " into jedis.keys(" * "). Kindly tell me some suggestions....
I know this is an old question, but hopefully the following will be useful for others.
Here's something I came up with as the most recent version of Jedis (3.2.0 as of this time) did not support the "memory usage " command which is available on Redis >= 4. This code assumes a Jedis object has been created, probably from a Jedis resource pool:
import redis.clients.jedis.util.SafeEncoder;
// ... Jedis setup code ...
byteSize = (Long) jedis.sendCommand(new ProtocolCommand() {
#Override
public byte[] getRaw() {
return SafeEncoder.encode("memory");
}},
SafeEncoder.encode("usage"),
SafeEncoder.encode(key));
This is a special case command as it has a primary keyword memory with a secondary action usage (other ones are doctor, stats, purge, etc). When sending multi-keyword commands to Redis, the keywords must be treated as a list. My first attempt at specifying memory usage as a single argument failed with a Redis server error.
Subsequently, it seems the current Jedis implementation is geared toward single keyword commands, as underneath the hood there's a bunch of special code to deal with multi-keyword commands such as debug object that doesn't quite fit the original command keyword framework.
Anyway, once my current project that required the ability to call memory usage is complete, I'll try my hand at providing a patch to the Jedis maintainer to implement the above command in a more official/conventional way, which would look something like:
Long byteSize = jedis.memoryUsage(key);
Finally, to address your specific need, you're best bet is to use the scan() method of the Jedis class. There are articles here on SO that explain how to use the scan() method.
Hmm...You can make the same thing by referring to the following.
redis.clients.jedis.Connection.sendCommand(Command, String...)
Create a class extend Connection.
Create a class extend Connection instance and call the connect() method.
Call super.sendCommand(Protocol.Command.valueOf(args[0].toUpperCase()), args[1~end]).
example for you:
public class JedisConn extends Connection {
public JedisConn(String host, int port) {
super(host, port);
}
#Override
protected Connection sendCommand(final Protocol.Command cmd, final String... args) {
return super.sendCommand(cmd, args);
}
public static void main(String[] args) {
JedisConn jedisConn = new JedisConn("host", 6379);
jedisConn.connect();
Connection connection = jedisConn.sendCommand(Protocol.Command.valueOf(args[0].toUpperCase()), Arrays.copyOfRange(args, 1, args.length));
System.out.println(connection.getAll());
jedisConn.close();
}
}
Haha~~
I have found a way for this. There is a function named eval(). We can use that for this as shown below.
`Scanner s=new Scanner(System.in);String query=s.nextLine();
String[] q=query.split(" ");
String cmd='\''+q[0]+'\'';
for(int i=1;i<q.length;i++)
cmd+=",\'"+q[i]+'\'';
System.out.println(j.eval("return redis.call("+cmd+")"));`

Passing the this pointer to a constructor

I'm trying to write a program in D that involves a Server class which creates new Client objects when new clients join. I want to pass the server object to the clients when they created, however when later I try to access the Server object from the client, my program stops with error code -11. I've googled it but found nothing.
I've successfully recreated this behavior in the following snippet:
import std.stdio;
class Server
{
public:
int n;
Client foo() //Foo creates a new client and passes this to it
{return new Client(this);}
}
class Client
{
public:
this(Server sv) //Constructor takes Server object
{sv=sv;}
Server sv;
void bar() //Function bar tries to access the server's n
{writeln(sv.n);}
}
void main()
{
Server s = new Server; //Create a new server object
Client c = s.foo(); //Then ask for a client
//c.sv=s; //!!!If I leave this line in the source then it works!!!
sv.n=5; //Set it to a random value
c.bar(); //Should print 5, but instead crashes w/ error -11
}
If I uncomment the c.sv=s line, then is magically works, which I don't understand.
So why is that if I set sv in the constructor then it crashes, but if I set it later then it works?
EDIT:
Adding writeln(sv) to the bar function prints null, so it can cause the crash. But why is it null?
{sv=sv;}
This line is the mistake. It sets the local sv, not the class instance. Try this.sv = sv; instead to set the instance member to the local.
edit: so since you never set the instance variable, it remains uninitialized - defaulting to null.

Setting up selenium webdriver for parallel execution

I am trying to execute a large suite of selenium tests via xUnit console runner in parallel.
These have executed and I see 3 chrome windows open, however the first send key commands simply executes 3 times to one window, resulting in test failure.
I have registered my driver in an objectcontainer before each scenario as below:
[Binding]
public class WebDriverSupport
{
private readonly IObjectContainer _objectContainer;
public WebDriverSupport(IObjectContainer objectContainer)
{
_objectContainer = objectContainer;
}
[BeforeScenario]
public void InitializeWebDriver()
{
var driver = GetWebDriverFromAppConfig();
_objectContainer.RegisterInstanceAs<IWebDriver>(driver);
}
And then call the driver in my specflow step defintions as:
_driver = (IWebDriver)ScenarioContext.Current.GetBindingInstance(typeof(IWebDriver));
ScenarioContext.Current.Add("Driver", _driver);
However this has made no difference and it seems as if my tests are trying to execute all commands to one driver.
Can anyone advise where I have gone wrong ?
You shouldn't be using ScenarioContext.Current in a parallel execution context. If you're injecting the driver through _objectContainer.RegisterInstanceAs you will receive it through constructor injection in your steps class' constructor, like so:
public MyScenarioSteps(IWebDriver driver)
{
_driver = driver;
}
More info:
https://github.com/techtalk/SpecFlow/wiki/Parallel-Execution#thread-safe-scenariocontext-featurecontext-and-scenariostepcontext
https://github.com/techtalk/SpecFlow/wiki/Context-Injection
In my opinion this is horribly messy.
This might not be an answer, but is too big for a comment.
why are you using the IObjectContainer if you are just getting it from the current scenario context and not injecting it via the DI mechanism? I would try this:
[Binding]
public class WebDriverSupport
{
[BeforeScenario]
public void InitializeWebDriver()
{
var driver = GetWebDriverFromAppConfig();
ScenarioContext.Current.Add("Driver",driver);
}
}
then in your steps:
_driver = (IWebDriver)ScenarioContext.Current.Get("Driver");
As long as GetWebDriverFromAppConfig returns a new instance you should be ok...

Selenium get value of current implicit wait

I realize that Selenium has a default value for implicit waits, but how do I get this value if I change it? For example:
driver.implicitly_wait( 13 );
How do I later get the 13 value from the driver?
Unfortunately there's no getter for that.
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html
There isn't for explicit waits either.
http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Wait.html
I know I'm a couple years late, and #JaneGoodall is not wrong -- there is no built-in function for that. But it's not impossible!
It's not very difficult to create your own versions of the WebDriver interface and browser-specific driver class. And then, you can put whatever code you want into the driver!
Example:
MyDriver.java (specialized version of WebDriver, not quite mandatory but a very good idea):
public interface MyDriver extends WebDriver {
void setWait(int timeout);
int getWait();
}
MyChrome.java (specialized version of ChromeDriver -- works the same for any browser)
public class MyChrome extends ChromeDriver implements MyDriver {
int timeout = 0;
public void setWait(int timeout) {
this.timeout = timeout;
this.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
}
public int getWait() {
return timeout;
}
}
And now, to use it, MyProgram.java:
public class MyProgram {
MyDriver driver = new MyChrome();
driver.setWait(10);
assert(driver.getWait() == 10);
}
I hope this is helpful!
For those who came here from google. In 2018 it seems like there is a method to get those timeouts at least in javascript (I know question was about java):
const {implicit, pageLoad, script} = await driver.manage().getTimeouts();
Hope this will help.
TL;DR - This is not a solution to get implicit waits. You cannot get the implicit wait in Java even today, without using a workaround like this.
In 2020, selenium 3.141.59 still does not have a getter for any timeouts. The WebDriver interface has a nested interface Timeouts which does not define any getters. RemoteWebDriver, which is the parent of Chrome and Firefox drivers, implements the WebDriver interface and it does not add a getter for timeouts.
RemoteWebDriver implements WebDriver.Timeouts, but it does not store the value of implicit timeout anywhere, as you can see from the code below.
protected class RemoteTimeouts implements Timeouts {
public Timeouts implicitlyWait(long time, TimeUnit unit) {
execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
"implicit", TimeUnit.MILLISECONDS.convert(time, unit)));
return this;
}
public Timeouts setScriptTimeout(long time, TimeUnit unit) {
execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
"script", TimeUnit.MILLISECONDS.convert(time, unit)));
return this;
}
public Timeouts pageLoadTimeout(long time, TimeUnit unit) {
execute(DriverCommand.SET_TIMEOUT, ImmutableMap.of(
"pageLoad", TimeUnit.MILLISECONDS.convert(time, unit)));
return this;
}
} // timeouts class.
The execute() method in the RemoteWebDriver takes the wait inside a Map of parameters, but it does not make that map or the wait settings accessible to us via a getter.
protected Response execute(String driverCommand, Map<String, ?> parameters)
//Open the source code to see why you can't make your own getter for implicitWait.
This can print real timeout value (plus calculating time, usually within 100ms):
public void getCurrentWaitTimeout() {
long milliseconds = java.time.ZonedDateTime.now().toInstant().toEpochMilli();
driver.findElements(By.cssSelector(".nonExistingElement"));
milliseconds = java.time.ZonedDateTime.now().toInstant().toEpochMilli() - milliseconds;
log.info("Current waiting timeout is {} milliseconds", milliseconds);
}
So you can always call such a method to be sure you know actual timeout, not the value you tried to set.
For Java version of Selenium, org.seleniumhq.selenium:selenium-api:4.0.0-beta-4
allows you to get the current implicit wait duration:
WebDriver.manage().timeouts().getImplicitWaitTimeout()
With this method, it makes possible to temporarily change the timeout to let's say 1 second and restore it afterwards:
final Duration originalTimeout = driver.manage().timeouts().getImplicitWaitTimeout();
driver.manage().timeouts().implicitlyWait(Duration.of(1, ChronoUnit.SECONDS));
... // do something
// restore the original timeout
driver.manage().timeouts().implicitlyWait(originalTimeout);
Probably, this functionality is present even before selenium-api:4.0.0-beta-4.
I get the defined implicitTimeout with:
driver.manage().timeouts().getImplicitWaitTimeout().getSeconds()
Many years later, in Python, using selenium 4.4.3 you can access the timeouts very easily via simply:
print(driver.timeouts.implicit_wait)
print(driver.timeouts.page_load)
print(driver.timeouts.script)
Note that when I call driver.implicitly_wait(60), it changed the implicit_wait value only.
Also, there is no such function as driver.manage() in Python, as in the Java answers above.

Glassfish - JEE6 - Use of Interceptor to measure performance

For measuring execution time of methods, I've seen suggestions to use
public class PerformanceInterceptor {
#AroundInvoke
Object measureTime(InvocationContext ctx) throws Exception {
long beforeTime = System.currentTimeMillis();
Object obj = null;
try {
obj = ctx.proceed();
return obj;
}
finally {
time = System.currentTimeMillis() - beforeTime;
// Log time
}
}
Then put
#Interceptors(PerformanceInterceptor.class)
before whatever method you want measured.
Anyway I tried this and it seems to work fine.
I also added a
public static long countCalls = 0;
to the PerformanceInterceptor class and a
countCalls++;
to the measureTime() which also seems to work o.k.
With my newby hat on, I will ask if my use of the countCalls is o.k. i.e
that Glassfish/JEE6 is o.k. with me using static variables in a Java class that is
used as an Interceptor.... in particular with regard to thread safety. I know that
normally you are supposed to synchronize setting of class variables in Java, but I
don't know what the case is with JEE6/Glassfish. Any thoughts ?
There is not any additional thread safety provided by container in this case. Each bean instance does have its own instance of interceptor. As a consequence multiple thread can access static countCalls same time.
That's why you have to guard both reads and writes to it as usual. Other possibility is to use AtomicLong:
private static final AtomicLong callCount = new AtomicLong();
private long getCallCount() {
return callCount.get();
}
private void increaseCountCall() {
callCount.getAndIncrement();
}
As expected, these solutions will work only as long as all of the instances are in same JVM, for cluster shared storage is needed.