Posts

Showing posts from 2017

Stubbing method which manipulates parameters using Mockito

Mockito is a mocking framework which enables mock creation, verification and stubbing. It has been widely used in Java community to write beautiful tests with its clean and simple API. Recently I was writing a unit test which involves stubbing a non-void method that manipulates the given parameter. The method would look like this: public interface Accessor { Data get(String dataId, Attribute attribute); } The get method will return the data associated with the given id and set the attribute of data accordingly. We can create the mock as usual: Accessor accessor = Mockito.mock(Accessor.class); Following code snippet shows how to manipulate the parameter in get method: Mockito.when(accessor.get(anyString(), any(Attribute.class))).thenAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { Object[] args = invocationOnMock.getArguments(); Attribute attribute = (Attribute) args[1]; // Set the attribute

Linux kernel documentation for /sys/class/net

Recently I am working on a project to handle network interface failure which involves detecting the connected state of a network cable. After searching around the Internet I found the post from Stack Overflow mentioned a good way to solve the problem. As Kent Fredric said,we can use /sys/class/net/<iface>/carrier and  /sys/class/net/<iface>/operstate to identify the connected state of a network cable. What does /sys/class/net/<iface>/carrier and /sys/class/net/<iface>/operstate means? From the kernel documentation I found the description for them: What: /sys/class/net/<iface>/operstate Date: March 2006 KernelVersion: 2.6.17 Contact: netdev@vger.kernel.org Description: Indicates the interface RFC2863 operational state as a string. Possible values are: "unknown", "notpresent", "down", "lowerlayerdown", "testing", "dormant", "up". What: /sys/class/net/<iface>/carr

Resolve issue ImportError: No module named thrift.Thrift when running python script

  When running python script which using struct and/or service defined in thrift, a problem shows up with the error message below:   Traceback (most recent call last):    File "./test.py", line 8, in <module>      from test import TestService    File "../thrift/gen-py/test/TestService.py", line 9, in <module>      from thrift.Thrift import TType, TMessageType, TException, TApplicationException    ImportError: No module named thrift.Thrift From the error message we can see that the python script test.py import a module TestService which is defined in a thrift and the thrift-generated python file is "../thrift/gen-py/test/TestService.py, and the file TestService.py imports TType, TMessageType, TException, TApplicationException and get ImportError. It must be a configuration issue since TType, TMessageType, TException, and TApplicationException should be included when Thrift is installed. After searching around in Google, I found that a blog

Errors: Linux System Errors

Source link: http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html When system requests fail, error code are returned. To understand the nature of the error these codes need to be interpreted. They are recorded in:- /usr/include/asm/errno.h Here is a copy of that file as of Aug 2004 on RedHat 7.3 #define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No such process */ #define EINTR 4 /* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO 6 /* No such device or address */ #define E2BIG 7 /* Arg list too long */ #define ENOEXEC 8 /* Exec format error */ #define EBADF 9 /* Bad file number */ #define ECHILD 10 /* No child processes */ #define EAGAIN 11 /* Try again */ #define ENOMEM