diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index d295fc1aa7..90da309dd7 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -339,6 +339,9 @@ func (m *MosnRuntime) initPubSubs(factorys ...*runtime_pubsub.Factory) error { // check consumerID consumerID := strings.TrimSpace(config.Metadata["consumerID"]) if consumerID == "" { + if config.Metadata == nil { + config.Metadata = make(map[string]string) + } config.Metadata["consumerID"] = m.runtimeConfig.AppManagement.AppId } // init this component with the config diff --git a/pkg/runtime/runtime_test.go b/pkg/runtime/runtime_test.go index f4fadb5a44..bab043c1c8 100644 --- a/pkg/runtime/runtime_test.go +++ b/pkg/runtime/runtime_test.go @@ -294,6 +294,32 @@ func TestMosnRuntime_initPubSubs(t *testing.T) { }) } +func TestMosnRuntime_initPubSubsNotExistMetadata(t *testing.T) { + t.Run("normal", func(t *testing.T) { + // mock pubsub component + mockPubSub := mock_pubsub.NewMockPubSub(gomock.NewController(t)) + mockPubSub.EXPECT().Init(gomock.Any()).Return(nil) + f := func() pubsub.PubSub { + return mockPubSub + } + + cfg := &MosnRuntimeConfig{ + PubSubManagement: map[string]mpubsub.Config{ + "mock": {}, + }, + } + // construct MosnRuntime + m := NewMosnRuntime(cfg) + m.errInt = func(err error, format string, args ...interface{}) { + log.DefaultLogger.Errorf("[runtime] occurs an error: "+err.Error()+", "+format, args...) + } + // test initPubSubs + err := m.initPubSubs(mpubsub.NewFactory("mock", f)) + // assert result + assert.Nil(t, err) + }) +} + func TestMosnRuntime_initStates(t *testing.T) { t.Run("init success", func(t *testing.T) { // prepare mock