Skip to content

Instantly share code, notes, and snippets.

View thinkerbot's full-sized avatar

Simon Chiang thinkerbot

View GitHub Profile
class NonFairQueue
attr_reader :nodes
attr_reader :edges
def initialize(options = {})
@nodes = options.fetch(:nodes, {}) # {name => {:command, :stdin_type, :stdout_type}}
@edges = options.fetch(:edges, {}) # {parent => [child]}
end
def run
@thinkerbot
thinkerbot / non-fair-queue
Last active December 22, 2015 14:49
Fast Multi-Process Nonfair-Queue via FIFOs, leveraging PIPE_BUF
# Fast Multi-Process Nonfair-Queue via FIFOs, leveraging PIPE_BUF
# ===============================================================
#
# Provided `ulimit -a` indicates pipe size is 512 bytes This will repeat a
# message n times and confirm all the messages are received intact after pipe
# traversal (ie PIPE_BUF is respected).
#
# Increasing the message size by 1 (511 -> 512 for a total length of 513) will
# demonstrate how the messages are interleaved.
#
@thinkerbot
thinkerbot / multiple_writers
Created September 1, 2013 18:39
Demonstrates that multiple writers to a single fifo often get inputs interleaved, regardless of sync.
#!/bin/bash
# 8... always?
mkfifo fifo
ruby -e '100000.times {|i| puts "a"}' > fifo &
ruby -e '100000.times {|i| puts "b"}' > fifo &
ruby -e '100000.times {|i| puts "c"}' > fifo &
ruby -e '100000.times {|i| puts "d"}' > fifo &
ruby -e '100000.times {|i| puts "e"}' > fifo &
ruby -e '100000.times {|i| puts "f"}' > fifo &
ruby -e '100000.times {|i| puts "g"}' > fifo &
#!/usr/bin/env ruby
r, w = IO.pipe
# exec on ruby 2.0 closes the fds and so cat fails with "Bad file descriptor"
# on ree this works as expected.
#
# I thought close_on_exec was the issue, but this doesn't fix it at all.
# Perhaps it is not being respected?
#
# r.close_on_exec = false
@thinkerbot
thinkerbot / gist:5935753
Created July 5, 2013 16:43
Variations of using a HEREDOC with a pipe and multiline strings
sed -e 's/a/A/' <<DOC | sed -e "\
s/b/B/
"
abc
DOC
(sed -e 's/a/A/' | sed -e "\
s/b/B/
") <<DOC
abc
@thinkerbot
thinkerbot / test_example
Created May 17, 2013 15:15
Stubbing on the command line.
#!/bin/sh
cat > example <<"DOC"
#!/usr/bin/env ruby
puts "got: #{`git log | head -n 1`}"
DOC
chmod +x example
test_example () {
./example | grep "got: commit [[:xdigit:]]\{40\}"
@thinkerbot
thinkerbot / README.md
Created March 15, 2013 03:48
shpec carryover examples

Illustrates carryover between shspec examples.

Carryover example to example:

$ shpec carryover_it_to_it.sh
shpec
  shares state between examples a
  shares state between examples b
  (Expected [] to equal [value])
@thinkerbot
thinkerbot / a.bash
Created February 20, 2013 04:19
The miracle of exec for passing ENV among programs.
#!/bin/bash
export A=a
exec ./b.rb
echo not here
interpolate () {
eval "
sed -e 's/^://' <<TEMPLATE
$(sed -e 's/^/:/' -e 's/\([`\]\)/\\\1/g' -e 's/$(/\\$(/g')
TEMPLATE
"
}
item=milk
cat <<<'got $item'
#!/bin/sh
sed -e '
s/\\/\\\\/g
s/'\''/\\'\''/g
s/$/\\n/g
s//\\001/g
s//\\002/g
s//\\003/g
s//\\004/g
s//\\005/g