Repository: snowflakedb/gosnowflake
Pull request #509 (2021-11-30 20:35:22)

Set the result type before calling exec from Ping
### Description
If the context has been set to Async Mode, the driver will attempt to
inspect the result type. If the result type has not been set, the driver
will panic when returning the [partial] result.

We ran into this issue when pinging a connection to test for aliveness when the context had `WithAsyncMode` applied. While async mode doesn't make a lot of sense for pings, in this case the same context is used to ping before executing a query.

Happy to write a test, just wasn't clear to me where Ping tests would live.

### Checklist
- [ ] Code compiles correctly
- [x] Run ``make fmt`` to fix inconsistent formats
- [x] Run ``make lint`` to get lint errors and fix all of them
- [ ] Created tests which fail without the change (if possible)
- [ ] All tests passing
- [ ] Extended the README / documentation, if necessary


sfc-gh-jbahk: Hi @nyergler , yes would you mind writing a test for this? Thank you.
nyergler: @sfc-gh-jbahk Of course; any chance you can point me to the right place for that to live? Wasn't immediately clear to me how `Ping` is currently being tested.
sfc-gh-jbahk: I think connection_test.go should suffice. Its tests are in driver_test.go at the moment, but I think it makes sense to move it under the former.
sfc-gh-pfus: Hello @nyergler ! Are you planning to rebase and provide a test for your solution? I'd like to merge it when it is ready and tested :)
nyergler: Thanks for the nudge, @sfc-gh-pfus ; I'll take a look at that this week.
github-actions[bot]: **CLA Assistant Lite bot:** <br/>Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you all sign our [Contributor License Agreement](https://github.com/snowflakedb/CLA/blob/main/README.md) before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.<br/>
   - - -
   I have read the CLA Document and I hereby sign the CLA
   - - -
   **0** out of **2** committers have signed the CLA.<br/>:x: @nyergler<br/>:x: @jc<br/><sub>You can retrigger this bot by commenting **recheck** in this Pull Request</sub>
nyergler: I have read the CLA Document and I hereby sign the CLA

nyergler: @sfc-gh-pfus Updated.
jc: I have read the CLA Document and I hereby sign the CLA


nyergler: recheck
sfc-gh-pfus: Hello @nyergler ! I approved your PR, but in the meantime a colleague of mine merged his PR. Since this is cross-repo PR I cannot rebase it myself. Can you rebase?
nyergler: @sfc-gh-pfus Done.

Patch:
diff --git a/connection.go b/connection.go
index c9d7603..a3c04a0 100644
--- a/connection.go
+++ b/connection.go
@@ -439,6 +439,7 @@ func (sc *snowflakeConn) Ping(ctx context.Context) error {
 	noResult := isAsyncMode(ctx)
 	isDesc := isDescribeOnly(ctx)
 	// TODO: handle isInternal
+	ctx = setResultType(ctx, execResultType)
 	_, err := sc.exec(ctx, "SELECT 1", noResult, false, /* isInternal */
 		isDesc, []driver.NamedValue{})
 	return err


Test patch:
diff --git a/async_test.go b/async_test.go
index 3d976a0..a071592 100644
--- a/async_test.go
+++ b/async_test.go
@@ -45,6 +45,22 @@ func TestAsyncMode(t *testing.T) {
 	})
 }
 
+func TestAsyncModePing(t *testing.T) {
+	ctx := WithAsyncMode(context.Background())
+
+	runDBTest(t, func(dbt *DBTest) {
+		defer func() {
+			if r := recover(); r != nil {
+				t.Fatalf("panic during ping: %v", r)
+			}
+		}()
+		err := dbt.conn.PingContext(ctx)
+		if err != nil {
+			t.Fatal(err)
+		}
+	})
+}
+
 func TestAsyncModeMultiStatement(t *testing.T) {
 	withMultiStmtCtx, _ := WithMultiStatement(context.Background(), 6)
 	ctx := WithAsyncMode(withMultiStmtCtx)