forked from samuel/go-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CreateMode: fixing incorrect create mode TTL including refactor of cr…
…eate flags BREAKING Client behavior: since this proposes a new parsing of the Create flag integer this will break clients if they rely on CreateContainer as it was creating znodes that may not have been containers. Changes: - Fix FlagTTL value from 4 -> 5 - Adding all known CreateMode values to Flag constants. - Adding a createMode private struct behind the flag integer, replicate CreateMode from ZK java lib. - Create, CreateContainer, CreateTTL methods now parse the flag integer passed in based on the constants defined. - Rewrite tests to better catch CreateContainer and CreateTTL (no assertions on zk behavior since zk does not expose znode mode) - Added Change-Detector unit tests for create mode values.
- Loading branch information
Showing
6 changed files
with
242 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package zk | ||
|
||
import "fmt" | ||
|
||
const ( | ||
FlagPersistent = 0 | ||
FlagEphemeral = 1 | ||
FlagSequence = 2 | ||
FlagEphemeralSequential = 3 | ||
FlagContainer = 4 | ||
FlagTTL = 5 | ||
FlagPersistentSequentialWithTTL = 6 | ||
) | ||
|
||
type createMode struct { | ||
flag int32 | ||
ephemeral bool | ||
sequential bool | ||
isContainer bool | ||
isTTL bool | ||
} | ||
|
||
func (cm *createMode) toFlag() int32 { | ||
return cm.flag | ||
} | ||
|
||
// parsing a flag integer into the CreateMode needed to call the correct | ||
// Create RPC to Zookeeper. | ||
// | ||
// NOTE: This parse method is designed to be able to copy and paste the same | ||
// CreateMode ENUM constructors from Java: | ||
// https://github.com/apache/zookeeper/blob/master/zookeeper-server/src/main/java/org/apache/zookeeper/CreateMode.java | ||
func parseCreateMode(flag int32) (createMode, error) { | ||
switch flag { | ||
case FlagPersistent: | ||
return createMode{0, false, false, false, false}, nil | ||
case FlagEphemeral: | ||
return createMode{1, true, false, false, false}, nil | ||
case FlagSequence: | ||
return createMode{2, false, true, false, false}, nil | ||
case FlagEphemeralSequential: | ||
return createMode{3, true, true, false, false}, nil | ||
case FlagContainer: | ||
return createMode{4, false, false, true, false}, nil | ||
case FlagTTL: | ||
return createMode{5, false, false, false, true}, nil | ||
case FlagPersistentSequentialWithTTL: | ||
return createMode{6, false, true, false, true}, nil | ||
default: | ||
return createMode{}, fmt.Errorf("invalid flag value: [%v]", flag) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package zk | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParseCreateMode(t *testing.T) { | ||
changeDetectorTests := []struct { | ||
name string | ||
flag int32 | ||
wantIntValue int32 | ||
}{ | ||
{"valid flag createmode 0 persistant", FlagPersistent, 0}, | ||
{"ephemeral", FlagEphemeral, 1}, | ||
{"sequential", FlagSequence, 2}, | ||
{"ephemeral sequential", FlagEphemeralSequential, 3}, | ||
{"container", FlagContainer, 4}, | ||
{"ttl", FlagTTL, 5}, | ||
{"persistentSequential w/TTL", FlagPersistentSequentialWithTTL, 6}, | ||
} | ||
for _, tt := range changeDetectorTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cm, err := parseCreateMode(tt.flag) | ||
requireNoError(t, err) | ||
if cm.toFlag() != tt.wantIntValue { | ||
// change detector test for enum values. | ||
t.Fatalf("createmode value of flag; want: %v, got: %v", cm.toFlag(), tt.wantIntValue) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("failed to parse", func(t *testing.T) { | ||
cm, err := parseCreateMode(-123) | ||
if err == nil { | ||
t.Fatalf("error expected, got: %v", cm) | ||
} | ||
if !strings.Contains(err.Error(), "invalid flag value") { | ||
t.Fatalf("unexpected error value: %v", err) | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters