From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>
Received: from mp11.migadu.com ([2001:41d0:2:bcc0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by ms5.migadu.com with LMTPS
	id OAYuFh8/6WPfHAEAbAwnHQ
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Sun, 12 Feb 2023 20:33:51 +0100
Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp11.migadu.com with LMTPS
	id 6E4rFh8/6WMiEwEA9RJhRA
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Sun, 12 Feb 2023 20:33:51 +0100
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by aspmx1.migadu.com (Postfix) with ESMTPS id 30474BFED
	for <larch@yhetil.org>; Sun, 12 Feb 2023 20:33:51 +0100 (CET)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <emacs-orgmode-bounces@gnu.org>)
	id 1pRI5r-0005AW-Tc; Sun, 12 Feb 2023 14:32:59 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <matt@excalamus.com>)
 id 1pRI5h-0005AG-6j
 for emacs-orgmode@gnu.org; Sun, 12 Feb 2023 14:32:50 -0500
Received: from sender3-op-o18.zoho.com ([136.143.184.18])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <matt@excalamus.com>)
 id 1pRI5f-0005RP-Jo
 for emacs-orgmode@gnu.org; Sun, 12 Feb 2023 14:32:48 -0500
ARC-Seal: i=1; a=rsa-sha256; t=1676230364; cv=none; 
 d=zohomail.com; s=zohoarc; 
 b=NBOxdzXS07+RPnC4taUOFvwh908mLXk1YU4tqziiDboezCYibiElaRSzs2C3kVGgDGLu0VKptAwcYX6fKe+ZngcFS+2Ls2ilhkoIKHLqSvoUNsuuVdaaNVDTvnQQIQYRpeFzEReuV0niHHlgpgIJGAZhU1pn4T3jxtHUu7aILDQ=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc; t=1676230364;
 h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To;
 bh=v6QNdtKOZFqQGACVdMqpS/tyti6pPuSO2Dn67JQDXf4=; 
 b=dJOy+Q6o8iLvgpraCEN3I1xGMZu+NE0b/cGnWBF1zywh56jRcXYaCNVSRoJl3neqiXE3jo0VN+yayhGP8IdCMSnMVLnrScYqVK37aD2vmB6hn5uHwCSyqUnyHLzlziKlTBB3FLVYNKsAu4y2+hCpPCzzrluc46zTJgt0/SwgYV0=
ARC-Authentication-Results: i=1; mx.zohomail.com;
 dkim=pass  header.i=excalamus.com;
 spf=pass  smtp.mailfrom=matt@excalamus.com;
 dmarc=pass header.from=<matt@excalamus.com>
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1676230364; 
 s=zmail; d=excalamus.com; i=matt@excalamus.com;
 h=Date:Date:From:From:To:To:Cc:Cc:Message-ID:In-Reply-To:References:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To;
 bh=v6QNdtKOZFqQGACVdMqpS/tyti6pPuSO2Dn67JQDXf4=;
 b=QXuyanhGhj2G2tNjhxXiQhtjNA8IH0nMM6MloA5EJvSXExsRzOrgHFTezlfUcrre
 917DPSg7EYzUiNf8NH02ZIoz5i8x9V62UPQr0uOmi//xnqaDszetltaFJ3DBzcrbumN
 Ttqn8DsKzGStSctnu1yP6O3jEidYJsD5HXyYn6kQ=
Received: from mail.zoho.com by mx.zohomail.com
 with SMTP id 167623036355140.456432106450734;
 Sun, 12 Feb 2023 11:32:43 -0800 (PST)
Date: Sun, 12 Feb 2023 14:32:43 -0500
From: Matt <matt@excalamus.com>
To: "Ihor Radchenko" <yantar92@posteo.net>
Cc: "emacs-orgmode" <emacs-orgmode@gnu.org>
Message-ID: <186471d8980.c36aecd0374444.2548279362371712281@excalamus.com>
In-Reply-To: <87fsbcbf47.fsf@localhost>
References: <186283d230a.129f5feb61660123.3289004102603503414@excalamus.com>
 <87v8kd8zzw.fsf@localhost>
 <1863472efe9.10fdd5ba4258906.5972264927968042941@excalamus.com>
 <87y1p7axpe.fsf@localhost>
 <1863d69188f.11e078bd1228881.451172865246440158@excalamus.com>
 <87fsbcbf47.fsf@localhost>
Subject: Re: [PATCH] Async evaluation in ob-shell
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
Importance: Medium
User-Agent: Zoho Mail
X-Mailer: Zoho Mail
Received-SPF: pass client-ip=136.143.184.18; envelope-from=matt@excalamus.com;
 helo=sender3-op-o18.zoho.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: emacs-orgmode@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "General discussions about Org-mode." <emacs-orgmode.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/emacs-orgmode>,
 <mailto:emacs-orgmode-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/emacs-orgmode>
List-Post: <mailto:emacs-orgmode@gnu.org>
List-Help: <mailto:emacs-orgmode-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/emacs-orgmode>,
 <mailto:emacs-orgmode-request@gnu.org?subject=subscribe>
Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org
Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org
X-Migadu-Flow: FLOW_IN
X-Migadu-Country: US
ARC-Seal: i=2; s=key1; d=yhetil.org; t=1676230431; a=rsa-sha256; cv=pass;
	b=r82hcz51CjUxehR0lUw+BnSVOSNhxh07ZQ02gy75EywgDOBo0p8Mqdz51KHKvh+a1uHdLY
	sHOlBAFNH5ODOV7yOdakG0QlL3PyxuBtVhvK3F6WsKXpkiG0s3YykccumnS7W8nnvvFx2i
	i3GsKx/tU61yaNGaaBxb+h99r9ikoShumDiDlNw+w19UdcKS0mw1ptmu0ERbbECLbpfGHt
	rTBJx9JngUUhmZ9NWPjXUoEqAwGT0wM3Mw8iZR5kpalWCcI3KmWxopi9ajXnUgGoXDMh1D
	ua2Su0iRFYL7zKf8+DE2inmMbxlmzO3hz24WC+jDs8LDLDa8oYmBcex9KhNKKA==
ARC-Authentication-Results: i=2;
	aspmx1.migadu.com;
	dkim=pass header.d=excalamus.com header.s=zmail header.b=QXuyanhG;
	dmarc=none;
	arc=pass ("zohomail.com:s=zohoarc:i=1");
	spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org;
	s=key1; t=1676230431;
	h=from:from:sender:sender:reply-to:subject:subject:date:date:
	 message-id:message-id:to:to:cc:cc:mime-version:mime-version:
	 content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references:list-id:list-help:
	 list-unsubscribe:list-subscribe:list-post:dkim-signature;
	bh=v6QNdtKOZFqQGACVdMqpS/tyti6pPuSO2Dn67JQDXf4=;
	b=oxVgTiBESspzdp/KZJL8NHpQQdR4CnBE6XXpPXLB6KIEqNMBFDsXuT3ZWXIHQjZMlt82pg
	PykONvp0F9KqDdWJ/5JfrbEy6vx3j+nyq8FNQKn+UcpEiXVapwN8QwuNjp+ASNCdHJbaW8
	OH/tVkqG1Cp1LfGVpzUMhshpH3ZNhHXsB/5TRLqLVqzsOhfdofiWZ7n0mYrjsE65k38gTm
	v4bueotZytCjqWRNIZy5dOp3r7nIYSuluML4ZqD4g+Qqr6RpoakLMwlJRsHKcqVjNIrcxx
	Y4IBt8WB9otInbuYIoI5qyQMHIgkB0Y8acDjDJT0eJzSAjIWlJ/G4GxGfrm7fQ==
X-Migadu-Queue-Id: 30474BFED
Authentication-Results: aspmx1.migadu.com;
	dkim=pass header.d=excalamus.com header.s=zmail header.b=QXuyanhG;
	dmarc=none;
	arc=pass ("zohomail.com:s=zohoarc:i=1");
	spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"
X-Migadu-Scanner: scn0.migadu.com
X-Migadu-Spam-Score: -8.36
X-Spam-Score: -8.36
X-TUID: 8fYjwj+G4K5T


 ---- On Sat, 11 Feb 2023 06:44:56 -0500  Ihor Radchenko  wrote --- 
 > 1. You should provide all the docstrings.
 > 2. I generally feel that separate async and separate session code are
 >    either duplicating the same code or edge cases considered by session
 >    code may popup in async code unexpectedly.

Excellent points.  Thank you.

As part of the commit, I want to include tests.

How to test an async block is non-obvious.  The initial evaluation returns a uuid which returns immediately and can be checked using a regex:

(defconst test-ob-shell/uuid-regex
  "[0-9a-fA-F]\\{8\\}\\b-[0-9a-fA-F]\\{4\\}\\b-[0-9a-fA-F]\\{4\\}\\b-[0-9a-fA-F]\\{4\\}\\b-[0-9a-fA-F]\\{12\\}")

Technically, this is a ob-comint aspect and may be more rightly included in (the currently non-existant) tests for that module.

Checking the final result from the callback is trickier.   The following works, but requires advice (which could potentially persist beyond the test) and a delay (which slows testing overall and whose duration likely depends on the hardware the test runs on).  Without the delay, I could not get the callback to execute within the test.  It would execute when running manually in an Org buffer, however.  I'm not sure why. 

(ert-deftest test-ob-shell/session-async-evaluation ()
  (let ((session-name "test-ob-shell/session-async-evaluation")
        (kill-buffer-query-functions nil)
        result)
    ;; perform check after the callback executes which looks for the
    ;; expected result
    (advice-add
     'ob-shell-async-chunk-callback
     :filter-return
     (lambda (&rest r)
       (let ((result (car r)))
         (if (not (string= result "1\n2\n"))
             (ert-fail (format "Expected 1\n2\n: %s" result)))
         result))
     `((name . ,session-name)))
    ;; always remove the advice, regardless of test outcome
    (unwind-protect
        (org-test-with-temp-text
            (concat "#+begin_src sh :session " session-name " :async t
echo 1
echo 2<point>
#+end_src")
          ;; execute the block; delay momentarily so that the callback
          ;; executes
          (setq result (org-trim (org-babel-execute-src-block)))
          (if (should
               (and
                ;; if the block runs...
                (string-match
                 test-ob-shell/uuid-regex
                 result)
                ;; ...and the callback executes without fail
                (not (sleep-for 0.1))))
              ;; clean up buffer on success
              (kill-buffer session-name)))
      (advice-remove 'ob-shell-async-chunk-callback session-name))))

This works for me using the last patch.

Thoughts?