You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "TSultanov (via GitHub)" <gi...@apache.org> on 2023/03/10 09:04:42 UTC

[GitHub] [beam] TSultanov opened a new pull request, #25791: Start Google Cloud Datastore emulator from Go tests automatically

TSultanov opened a new pull request, #25791:
URL: https://github.com/apache/beam/pull/25791

   Start Google Datastore emulator from Go tests automatically
   
   addresses #25588
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25791:
URL: https://github.com/apache/beam/pull/25791#issuecomment-1480951598

   Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment `assign set of reviewers`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25791:
URL: https://github.com/apache/beam/pull/25791#issuecomment-1495867789

   Assigning new set of reviewers because Pr has gone too long without review. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @damccorm for label build.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163790549


##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()

Review Comment:
   I think it would just move the same problem but to a different place - any error during `Client.Close()` code will prevent a call to `emulator.Stop()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163840136


##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()
+	if clientCloseErr != nil {
+		return clientCloseErr
+	}
+	if emulatorStopErr != nil {
+		return emulatorStopErr
+	}
+	return nil
+}
+
+func NewEmulated(ctx context.Context) (EmulatedDatastore, error) {
+	emulator, err := startEmulator()
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	if _, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
+		err := os.Unsetenv("GOOGLE_CLOUD_PROJECT")
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	if err := os.Setenv(constants.EmulatorHostKey, emulator.GetEndpoint()); err != nil {
+		panic(err)
+	}
+
+	datastoreDb, err := New(ctx, mapper.NewPrecompiledObjectMapper(), constants.EmulatorProjectId)
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	return EmulatedDatastore{Datastore: datastoreDb, emulator: emulator}, nil
+}
+
+func startEmulator() (*emulator, error) {

Review Comment:
   If you I understand you correctly, you basically are suggesting to use context cancellation to kill the emulator process, right?
   I've tried this approach, but sadly it doesn't work that well as cancelling the context only terminates the first process in the child process tree (starting the datastore emulator creates at least three processes), and the actual emulator process gets reparented by the `init`. The current code sends SIGTERM to the whole child process group to terminate the whole process tree.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damccorm merged pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm merged PR #25791:
URL: https://github.com/apache/beam/pull/25791


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163840136


##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()
+	if clientCloseErr != nil {
+		return clientCloseErr
+	}
+	if emulatorStopErr != nil {
+		return emulatorStopErr
+	}
+	return nil
+}
+
+func NewEmulated(ctx context.Context) (EmulatedDatastore, error) {
+	emulator, err := startEmulator()
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	if _, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
+		err := os.Unsetenv("GOOGLE_CLOUD_PROJECT")
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	if err := os.Setenv(constants.EmulatorHostKey, emulator.GetEndpoint()); err != nil {
+		panic(err)
+	}
+
+	datastoreDb, err := New(ctx, mapper.NewPrecompiledObjectMapper(), constants.EmulatorProjectId)
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	return EmulatedDatastore{Datastore: datastoreDb, emulator: emulator}, nil
+}
+
+func startEmulator() (*emulator, error) {

Review Comment:
   If you I understand you correctly, you basically are suggesting to use context cancellation to kill the emulator process, right?
   I've tried this approach, but sadly it doesn't work that well as cancelling the context only terminates the first process in the child process tree (starting the datastore emulator creates at least three processes), and the actual emulator process gets reparented by the `init`. The current code works around this problem by creating a process group out of all emulator processes and sending the SIGTERM to the whole group.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163840136


##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()
+	if clientCloseErr != nil {
+		return clientCloseErr
+	}
+	if emulatorStopErr != nil {
+		return emulatorStopErr
+	}
+	return nil
+}
+
+func NewEmulated(ctx context.Context) (EmulatedDatastore, error) {
+	emulator, err := startEmulator()
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	if _, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
+		err := os.Unsetenv("GOOGLE_CLOUD_PROJECT")
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	if err := os.Setenv(constants.EmulatorHostKey, emulator.GetEndpoint()); err != nil {
+		panic(err)
+	}
+
+	datastoreDb, err := New(ctx, mapper.NewPrecompiledObjectMapper(), constants.EmulatorProjectId)
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	return EmulatedDatastore{Datastore: datastoreDb, emulator: emulator}, nil
+}
+
+func startEmulator() (*emulator, error) {

Review Comment:
   If I understand you correctly, you basically are suggesting to use context cancellation to kill the emulator process, right?
   I've tried this approach, but sadly it doesn't work that well as cancelling the context only terminates the first process in the child process tree (starting the datastore emulator creates at least three processes), and the actual emulator process gets reparented by the `init`. The current code works around this problem by creating a process group out of all emulator processes and sending the SIGTERM to the whole group.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25791:
URL: https://github.com/apache/beam/pull/25791#issuecomment-1490199432

   Reminder, please take a look at this pr: @Abacn 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25791:
URL: https://github.com/apache/beam/pull/25791#issuecomment-1481030877

   Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @Abacn for label build.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163392187


##########
playground/backend/internal/db/schema/migration/migrations_test.go:
##########
@@ -61,8 +57,9 @@ func setup() {
 }
 
 func teardown() {
-	if err := datastoreDb.Client.Close(); err != nil {
-		panic(err)
+	clientCloseErr := datastoreDb.Close()
+	if clientCloseErr != nil {
+		panic(clientCloseErr)

Review Comment:
   Perhaps these errors could be collected in chan error and handled like:
   
   ```
   for {
      select {
         case err := <- clientErr:
              // gracefully shut down other processes
              return err
        case err := <- datastoreDbErr:
              // gracefully shut down other processes
              return err
      }
   }
   ```



##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()
+	if clientCloseErr != nil {
+		return clientCloseErr
+	}
+	if emulatorStopErr != nil {
+		return emulatorStopErr
+	}
+	return nil
+}
+
+func NewEmulated(ctx context.Context) (EmulatedDatastore, error) {
+	emulator, err := startEmulator()
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	if _, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
+		err := os.Unsetenv("GOOGLE_CLOUD_PROJECT")
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	if err := os.Setenv(constants.EmulatorHostKey, emulator.GetEndpoint()); err != nil {
+		panic(err)
+	}
+
+	datastoreDb, err := New(ctx, mapper.NewPrecompiledObjectMapper(), constants.EmulatorProjectId)
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	return EmulatedDatastore{Datastore: datastoreDb, emulator: emulator}, nil
+}
+
+func startEmulator() (*emulator, error) {

Review Comment:
   It may make your code cleaner and easy to troubleshoot if you receive a `ctx context.Context` and use the pattern:
   ```
   ctx, cancel := context.WithCancel(ctx)
   defer cancel()
   ```
   
   The test could perhaps do:
   ```
   ctx, cancel := context.WithTimeout(...)
   ```
   
   Would you like me to create an example?  Please let me know.



##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()

Review Comment:
   Usually in go it makes sense to evaluate one error at a time.  I wonder if it makes sense to instead call Client.Close and emulator.Stop within the test code itself, or some shared teardown method used by all the tests.



##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()
+	if clientCloseErr != nil {
+		return clientCloseErr
+	}
+	if emulatorStopErr != nil {
+		return emulatorStopErr
+	}
+	return nil
+}
+
+func NewEmulated(ctx context.Context) (EmulatedDatastore, error) {
+	emulator, err := startEmulator()
+	if err != nil {
+		return EmulatedDatastore{}, err
+	}
+
+	if _, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
+		err := os.Unsetenv("GOOGLE_CLOUD_PROJECT")
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	if err := os.Setenv(constants.EmulatorHostKey, emulator.GetEndpoint()); err != nil {
+		panic(err)
+	}
+
+	datastoreDb, err := New(ctx, mapper.NewPrecompiledObjectMapper(), constants.EmulatorProjectId)
+	if err != nil {
+		return EmulatedDatastore{}, err

Review Comment:
   Perhaps return a pointer instead of a struct so that the code follows the convention: `return nil, err`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1163790549


##########
playground/backend/internal/db/datastore/emulator_wrapper.go:
##########
@@ -0,0 +1,182 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/constants"
+	"beam.apache.org/playground/backend/internal/db/mapper"
+	"beam.apache.org/playground/backend/internal/logger"
+	"context"
+	"fmt"
+	"math/rand"
+	"net"
+	"os"
+	"os/exec"
+	"syscall"
+	"time"
+)
+
+const (
+	emulatorHost    = "127.0.0.1"
+	portRangeStart  = 10000
+	portRangeEnd    = 20000
+	maxAttempts     = 3
+	startupDuration = 500 * time.Millisecond
+	pauseDuration   = 100 * time.Millisecond
+	waitDuration    = 10 * time.Second
+)
+
+type EmulatedDatastore struct {
+	*Datastore
+	emulator *emulator
+}
+
+type emulator struct {
+	Host string
+	Port int
+	cmd  *exec.Cmd
+}
+
+func (ed EmulatedDatastore) Close() error {
+	clientCloseErr := ed.Datastore.Client.Close()
+	emulatorStopErr := ed.emulator.Stop()

Review Comment:
   I think it would just move the same problem but to a different place. - any error during `Client.Close()` code will prevent a call to `emulator.Stop()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] TSultanov commented on a diff in pull request #25791: [Playground] Start Google Cloud Datastore emulator from Go tests automatically

Posted by "TSultanov (via GitHub)" <gi...@apache.org>.
TSultanov commented on code in PR #25791:
URL: https://github.com/apache/beam/pull/25791#discussion_r1164243700


##########
playground/backend/internal/db/schema/migration/migrations_test.go:
##########
@@ -61,8 +57,9 @@ func setup() {
 }
 
 func teardown() {
-	if err := datastoreDb.Client.Close(); err != nil {
-		panic(err)
+	clientCloseErr := datastoreDb.Close()
+	if clientCloseErr != nil {
+		panic(clientCloseErr)

Review Comment:
   We don't have any other processes here and I don't think there is much merit in complicating the error handling situation here with channels. Current code, while not exactly following Go conventions to the letter, is cleaner in my opinion.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org