File: bug.rb

package info (click to toggle)
ruby-oj 2.17.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 968 kB
  • ctags: 1,672
  • sloc: ansic: 10,658; ruby: 6,524; makefile: 2
file content (64 lines) | stat: -rwxr-xr-x 1,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
# encoding: UTF-8

$: << File.dirname(__FILE__)

require 'helper'

require 'oj'
require 'securerandom'

class Handler
  def hash_start()      {} end
  def hash_set(h,k,v)   h.store(k,v) end
  def array_start()     [] end
  def array_append(a,v) a << v end
  def error(message, line, column)
    raise Exception.new(message, line, column)
  end
end

json = Oj.dump({"this"=>"object"})

if true
  name = "/tmp/#{SecureRandom.uuid}"
  `mkfifo #{name}`
  if fork
    open(name, 'r+') do |read_io|
      p "start reading #{read_io.stat.ftype}"
      Oj.sc_parse(Handler.new, read_io) {|v| p v}
      p "stop reading"
    end
  else
    open(name, 'w+') do |write_io|
      p "start writing #{write_io.stat.ftype}  autoclose: #{write_io.autoclose?}"
      write_io.write json
      write_io.write json
      p "stop writing"
    end
    sleep(1) # make it obvious that there are two threads
    open(name, 'w+') do |write_io|
      p "start writing #{write_io.stat.ftype}"
      write_io.write json
      write_io.write json
      p "stop writing"
    end
  end
else
  IO.pipe do |read_io, write_io|
    if fork
      write_io.close
      p "start reading #{read_io.stat.ftype}"
      Oj.sc_parse(Handler.new, read_io) {|v| p v}
      p "stop reading"
      read_io.close
    else
      read_io.close
      p "start writing #{write_io.stat.ftype}"
      write_io.write json
      write_io.write json
      p "stop writing"
      write_io.close
    end
  end
end