Skip to content

Commit

Permalink
tests for nsqd metadata migration to new filename
Browse files Browse the repository at this point in the history
  • Loading branch information
ploxiln committed Jan 9, 2017
1 parent 8ac9734 commit b715e6e
Showing 1 changed file with 108 additions and 1 deletion.
109 changes: 108 additions & 1 deletion nsqd/nsqd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ const (
RequestTimeout = 5 * time.Second
)

func newMetadataFile(opts *Options) string {
return path.Join(opts.DataPath, "nsqd.dat")
}
func oldMetadataFile(opts *Options) string {
return path.Join(opts.DataPath, fmt.Sprintf("nsqd.%d.dat", opts.ID))
}

func getMetadata(n *NSQD) (*meta, error) {
fn := path.Join(n.getOpts().DataPath, "nsqd.dat")
fn := newMetadataFile(n.getOpts())
data, err := ioutil.ReadFile(fn)
if err != nil {
return nil, err
Expand Down Expand Up @@ -158,6 +165,106 @@ func TestStartup(t *testing.T) {
<-doneExitChan
}

func TestMetadataMigrate(t *testing.T) {
old_meta := `
{
"topics": [
{
"channels": [
{"name": "c1", "paused": false},
{"name": "c2", "paused": false}
],
"name": "t1",
"paused": false
}
],
"version": "1.0.0-alpha"
}`

tmpDir, err := ioutil.TempDir("", "nsq-test-")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpDir)

opts := NewOptions()
opts.DataPath = tmpDir
opts.Logger = test.NewTestLogger(t)

oldFn := oldMetadataFile(opts)
err = ioutil.WriteFile(oldFn, []byte(old_meta), 0700)
if err != nil {
panic(err)
}

_, _, nsqd := mustStartNSQD(opts)
err = nsqd.LoadMetadata()
test.Nil(t, err)
err = nsqd.PersistMetadata()
test.Nil(t, err)
nsqd.Exit()

oldFi, err := os.Lstat(oldFn)
test.Nil(t, err)
test.Equal(t, oldFi.Mode()&os.ModeType, os.ModeSymlink)

_, _, nsqd = mustStartNSQD(opts)
err = nsqd.LoadMetadata()
test.Nil(t, err)

t1, err := nsqd.GetExistingTopic("t1")
test.Nil(t, err)
test.NotNil(t, t1)
c2, err := t1.GetExistingChannel("c2")
test.Nil(t, err)
test.NotNil(t, c2)

nsqd.Exit()
}

func TestMetadataConflict(t *testing.T) {
old_meta := `
{
"topics": [{
"name": "t1", "paused": false,
"channels": [{"name": "c1", "paused": false}]
}],
"version": "1.0.0-alpha"
}`
new_meta := `
{
"topics": [{
"name": "t2", "paused": false,
"channels": [{"name": "c2", "paused": false}]
}],
"version": "1.0.0-alpha"
}`

tmpDir, err := ioutil.TempDir("", "nsq-test-")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpDir)

opts := NewOptions()
opts.DataPath = tmpDir
opts.Logger = test.NewTestLogger(t)

err = ioutil.WriteFile(oldMetadataFile(opts), []byte(old_meta), 0700)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(newMetadataFile(opts), []byte(new_meta), 0700)
if err != nil {
panic(err)
}

_, _, nsqd := mustStartNSQD(opts)
err = nsqd.LoadMetadata()
test.NotNil(t, err)
nsqd.Exit()
}

func TestEphemeralTopicsAndChannels(t *testing.T) {
// ephemeral topics/channels are lazily removed after the last channel/client is removed
opts := NewOptions()
Expand Down

0 comments on commit b715e6e

Please sign in to comment.