steenzout-sqnc 1.0.6 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,15 @@ module Steenzout
4
4
 
5
5
  private
6
6
 
7
- def self.increment_and_store name
7
+ # Increments the counter and stores it into a file.
8
+ # If the current counter is not already in memory,
9
+ # it will read the initial value from the sequence file.
10
+ # If the sequence file is empty, it will set the counter
11
+ # to the configured offset and increment it.
12
+ #
13
+ # @param name: the name of the sequence.
14
+ #
15
+ def self.increment_and_store(name)
8
16
 
9
17
  open(@config[:sequences][name][:location], File::RDWR|File::CREAT, 0644) do |f|
10
18
 
@@ -12,18 +20,28 @@ module Steenzout
12
20
 
13
21
  f.flock(File::LOCK_EX)
14
22
 
23
+ # read counter from memory or disk
15
24
  value = @sequences[name] # get value from memory
16
25
  value = f.readline().to_i if @sequences[name].nil? rescue EOFError # get value from file
26
+
27
+ # initialize counter to offset, if needed
17
28
  value ||= @config[:sequences][name][:offset] # initialize value to offset if none of the above had a value
29
+ # increment
18
30
  value += @config[:sequences][name][:step]
19
- @sequences[name] = value
31
+
32
+ # write new counter into file
33
+ f.rewind if f.pos > 0
20
34
  f << value
35
+ f.truncate(f.pos)
36
+
37
+ @sequences[name] = value
38
+
21
39
  return value
22
40
 
23
41
  ensure
24
42
  f.flock(File::LOCK_UN)
25
43
  f.close
26
- end
44
+ end
27
45
 
28
46
  end
29
47
  end
data/lib/sqnc/manager.rb CHANGED
@@ -2,13 +2,13 @@ module Steenzout
2
2
 
3
3
  class SequenceManager
4
4
 
5
- class << self; attr_accessor :sequences end
6
- class << self; attr_accessor :implementations end
7
-
8
5
  @config = nil
9
6
  @sequences = {}
10
7
  @implementations = [:file]
11
8
 
9
+ class << self; attr_accessor :sequences end
10
+ class << self; attr_accessor :implementations end
11
+
12
12
 
13
13
  private
14
14
 
@@ -54,6 +54,21 @@ module Steenzout
54
54
 
55
55
  public
56
56
 
57
+ # Returns the current in-memory value for this sequence.
58
+ # WARNING: if the sequence hasn't been used yet, a nil value will be returned.
59
+ #
60
+ # @param name: the sequence name.
61
+ #
62
+ def self.current_value(name)
63
+
64
+ raise ArgumentError.new "The sequence #{name} doesn't exist!" if !@sequences.has_key? name
65
+
66
+ return @sequences[name]
67
+
68
+ end
69
+
70
+
71
+
57
72
  # Returns the list of allowed sequence management implementations.
58
73
  #
59
74
  def self.implementations
@@ -62,6 +77,17 @@ module Steenzout
62
77
 
63
78
 
64
79
 
80
+ # Increments and returns the given sequence by step (or offset + step if it has never been used before).
81
+ #
82
+ # @param name: the sequence name.
83
+ #
84
+ def self.increment(name)
85
+ raise ArgumentError.new "The sequence #{name} doesn't exist!" if !@sequences.has_key? name
86
+ self.increment_and_store name
87
+ end
88
+
89
+
90
+
65
91
  # Validates the configuration and loads the class with the given implementation.
66
92
  #
67
93
  def self.load
@@ -97,31 +123,6 @@ module Steenzout
97
123
 
98
124
 
99
125
 
100
- # Returns the current in-memory value for this sequence.
101
- # WARNING: if the sequence hasn't been used yet, a nil value will be returned.
102
- #
103
- # @param name: the sequence name.
104
- #
105
- def self.current_value name
106
-
107
- raise ArgumentError.new "The sequence #{name} doesn't exist!" if !@sequences.has_key? name
108
-
109
- return @sequences[name]
110
-
111
- end
112
-
113
-
114
-
115
- # Increments and returns the given sequence by step (or offset + step if it has never been used before).
116
- #
117
- # @param name: the sequence name.
118
- #
119
- def self.increment name
120
- raise ArgumentError.new "The sequence #{name} doesn't exist!" if !@sequences.has_key? name
121
- self.increment_and_store name
122
- end
123
-
124
-
125
126
  # Returns a list of the current sequence names being managed.
126
127
  #
127
128
  def self.sequences
@@ -13,30 +13,23 @@ require 'lib/steenzout-sqnc'
13
13
  class TestSequenceManager < Test::Unit::TestCase
14
14
 
15
15
  def setup
16
+
16
17
  @config = Steenzout::ConfigurationManager.configuration_for_gem 'steenzout-sqnc'
18
+
19
+ # default sequence manager
20
+ @config[:implementation] = :file
21
+
22
+ # add value to a sequence file
23
+ File.open(@config[:sequences][:seq3][:location], 'w') {|f| f.write('111')}
24
+
17
25
  end
18
26
 
19
27
  def teardown
28
+
20
29
  @config[:sequences].each_key {|name|
21
30
  sequence_file = @config[:sequences][name][:location]
22
31
  File.delete sequence_file if File.exist? sequence_file
23
32
  }
24
- end
25
-
26
-
27
-
28
- def __do_increment_tests
29
-
30
- assert_equal 1, Steenzout::SequenceManager::increment(:seq1), @config[:implementation]
31
- assert_equal 2, Steenzout::SequenceManager::increment(:seq1), @config[:implementation]
32
-
33
- # invalid sequence name
34
- assert_raises ArgumentError do
35
- Steenzout::SequenceManager::increment 'seq1'
36
- end
37
- assert_raises ArgumentError do
38
- Steenzout::SequenceManager::increment 'unknown'
39
- end
40
33
 
41
34
  end
42
35
 
@@ -45,7 +38,7 @@ class TestSequenceManager < Test::Unit::TestCase
45
38
  def test_increment
46
39
 
47
40
  # run tests for all implementations
48
- Steenzout::SequenceManager.implementations {|implementation|
41
+ Steenzout::SequenceManager.implementations.each {|implementation|
49
42
 
50
43
  # override configuration
51
44
  @config[:implementation] = implementation
@@ -54,18 +47,75 @@ class TestSequenceManager < Test::Unit::TestCase
54
47
  Steenzout::SequenceManager.load
55
48
 
56
49
  # run tests
57
- __do_increment_tests
50
+ # no step and no offset defined
51
+ sequence_file1 = @config[:sequences][:seq1][:location]
52
+
53
+ assert_equal 1, Steenzout::SequenceManager::increment(:seq1), @config[:implementation]
54
+ assert_equal 1, File.read(sequence_file1).to_i
55
+
56
+ assert_equal 2, Steenzout::SequenceManager::increment(:seq1), @config[:implementation]
57
+ assert_equal 2, File.read(sequence_file1).to_i
58
+
59
+
60
+ # step=100 and offset=100
61
+ sequence_file2 = @config[:sequences][:seq2][:location]
62
+
63
+ assert_equal 200, Steenzout::SequenceManager::increment(:seq2), @config[:implementation]
64
+ assert_equal 200, File.read(sequence_file2).to_i
65
+
66
+ assert_equal 300, Steenzout::SequenceManager::increment(:seq2), @config[:implementation]
67
+ assert_equal 300, File.read(sequence_file2).to_i
58
68
  }
59
69
 
60
70
  end
61
71
 
62
72
 
63
73
 
74
+ def test_increment_with_file_containing_initial_value
75
+
76
+ # override configuration
77
+ @config[:implementation] = :file
78
+
79
+ # load new sequence manager implementation
80
+ Steenzout::SequenceManager.load
81
+
82
+ # run tests
83
+ sequence_file3 = @config[:sequences][:seq3][:location]
84
+
85
+ assert_equal 112, Steenzout::SequenceManager::increment(:seq3), @config[:implementation]
86
+ assert_equal 112, File.read(sequence_file3).to_i
87
+
88
+ assert_equal 113, Steenzout::SequenceManager::increment(:seq3), @config[:implementation]
89
+ assert_equal 113, File.read(sequence_file3).to_i
90
+
91
+ end
92
+
93
+
94
+
95
+ def test_increment_with_invalid_sequence_names
96
+
97
+ # invalid sequence name
98
+ assert_raises ArgumentError do
99
+ Steenzout::SequenceManager::increment 'seq1'
100
+ end
101
+ assert_raises ArgumentError do
102
+ Steenzout::SequenceManager::increment 'unknown'
103
+ end
104
+
105
+ end
106
+
107
+
108
+
64
109
  def test_sequences
65
110
 
66
- sequence_names = []
67
- Steenzout::SequenceManager.sequences {|name| sequence_names << name}
68
- assert_equal([:seq1, :seq2], sequence_names)
111
+ result = []
112
+ Steenzout::SequenceManager.sequences {|name| result << name.to_s}
113
+ result.sort!
114
+
115
+ expected = ['seq1', 'seq2', 'seq3']
116
+ expected.sort!
117
+
118
+ assert_equal expected, result
69
119
 
70
120
  end
71
121
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steenzout-sqnc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 6
10
- version: 1.0.6
9
+ - 8
10
+ version: 1.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - steenzout