FileHelper { public static int poll(StructPollfd[] fds, int timeout) throws ErrnoException, InterruptedException { while (true) { if (Thread.interrupted()) throw new InterruptedException(); try { return Os.poll(fds, timeout); } catch (ErrnoException e) { if (e.errno == OsConstants.EINTR) continue; throw e; } } }  static InputStream openRead(Context context, String filename); static OutputStream openWrite(Context context, String filename); static Configuration loadCurrentSettings(Context context); static Configuration loadPreviousSettings(Context context); static Configuration loadDefaultSettings(Context context); static void writeSettings(Context context, Configuration config); static File getItemFile(Context context, Configuration.Item item); static InputStreamReader openItemFile(Context context, Configuration.Item item); static int poll(StructPollfd[] fds, int timeout); static FileDescriptor closeOrWarn(FileDescriptor fd, String tag, String message); static T closeOrWarn(T fd, String tag, String message);  }
@Test @PrepareForTest({Log.class, Os.class}) public void testPoll_success() throws Exception { mockStatic(Log.class); mockStatic(Os.class); when(Os.poll(any(StructPollfd[].class), anyInt())).then(new CountingAnswer(42)); assertEquals(42, FileHelper.poll(null, 0)); assertEquals(1, testResult); }
@Test @PrepareForTest({Log.class, Os.class}) public void testPoll_retryInterrupt() throws Exception { mockStatic(Log.class); mockStatic(Os.class); when(Os.poll(any(StructPollfd[].class), anyInt())).then(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { if (testResult++ == 0) { throw new ErrnoException("poll", OsConstants.EINTR); } return 0; } }); assertEquals(0, FileHelper.poll(null, 0)); assertEquals(2, testResult); }
@Test public void testPoll_interrupted() throws Exception { Thread.currentThread().interrupt(); try { FileHelper.poll(null, 0); fail("Did not interrupt"); } catch (InterruptedException e) { } }
@Test @PrepareForTest({Log.class, Os.class}) public void testPoll_fault() throws Exception { mockStatic(Log.class); mockStatic(Os.class); final ErrnoException e = new ErrnoException("foo", 42); e.getClass().getDeclaredField("errno").setInt(e, 42); when(Os.poll(any(StructPollfd[].class), anyInt())).then(new Answer<Integer>() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { testResult++; throw e; } }); try { FileHelper.poll(null, 0); fail("Did not throw"); } catch (ErrnoException e1) { assertEquals(42, e1.errno); assertSame(e, e1); } assertEquals(1, testResult); }