Skip to content

Instantly share code, notes, and snippets.

@bcoughlan
bcoughlan / MockUtils.java
Last active May 29, 2024 23:19
Mock an interface by proxying to a a real class
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
public class MockUtils {
/**
* Proxy an interface to a real implementation. The implementation doesn't have to implement the interface,
* which can save on a lot of boilerplate for test doubles.
*/
public static <T> T proxyToFake(Class<T> iface, Object forwardTo) {
@bcoughlan
bcoughlan / README.md
Created February 21, 2020 00:01
kubeadm: Enable service topology in 1.17
@bcoughlan
bcoughlan / forward.sh
Last active February 10, 2020 11:23
SSH forward a remote Docker daemon in a bash script
function ssh_forward_docker_daemon {
# Create a temporary file and delete. We just want the filename.
# Needed for the cleanup to identify which SSH process to kill
SSH_SOCKET=$(mktemp)
rm $SSH_SOCKET
# Clean up on exit
trap "exit" INT TERM
trap "ssh -S $SSH_SOCKET -O exit $1" EXIT
@bcoughlan
bcoughlan / README.md
Last active December 12, 2017 10:51
Tail multiple log files with filename

The output of tailing multiple files looks like this:

==> ./tomcat/catalina.2017-11-09.log <==
09-Nov-2017 17:53:31.496 WARNING [localhost-startStop-1] com.sun.faces.mgbean.BeanManager.addBean JSF1074
09-Nov-2017 17:53:31.496 WARNING [localhost-startStop-1] com.sun.faces.mgbean.BeanManager.addBean JSF1074
09-Nov-2017 17:53:31.496 WARNING [localhost-startStop-1] com.sun.faces.mgbean.BeanManager.addBean JSF1074
==> ./tomcat/catalina.2017-11-11.log <==
11-Nov-2017 17:53:31.496 WARNING [localhost-startStop-1] com.sun.faces.mgbean.BeanManager.addBean JSF1074
11-Nov-2017 17:53:31.496 WARNING [localhost-startStop-1] com.sun.faces.mgbean.BeanManager.addBean JSF1074
@bcoughlan
bcoughlan / ReplaceAlgorithms.java
Created April 14, 2015 23:44
Replacing a SecureRandom.getInstance() algorithm in Java for mocking
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.SecureRandomSpi;
import java.security.Security;
public class ReplaceAlgorithms {
public static void main(String[] args) throws NoSuchAlgorithmException {
byte[] bytes = new byte[16];
@bcoughlan
bcoughlan / lock.py
Created December 4, 2013 06:31
Python global lock using files
from contextlib import contextmanager
class LockError(Exception): pass
@contextmanager
def lock(lock_filename, force_lock=False):
forced = False
lockerror = False
try:
@bcoughlan
bcoughlan / HTTPFileServer.py
Last active December 20, 2015 23:48
Python HTTP file server for writing tests that retrieve files
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
import httplib
import threading
from os.path import join, abspath
class RequestHandler(SimpleHTTPRequestHandler):
def do_QUIT (self):
self.send_response(200)
self.end_headers()
@bcoughlan
bcoughlan / countduplicates
Created July 2, 2012 02:09
OpenOffice calc - count duplicates
=COUNTA(A2:A23)-SUMPRODUCT((A2:A23<>"")/(COUNTIF(A2:A23,A2:A23)+(A2:A23="")))
@bcoughlan
bcoughlan / humanize.js
Created June 15, 2012 06:08
Javascript - Format seconds to (hh:m)m:ss
function formatTime (t) {
if (typeof (t) !== "number" || t < 0) {
return "";
}
function it(param) {
var p = t % param;
if (p < 10) p = '0' + p;
t = Math.floor(t / param);
return p;
}